πβ μ λ¬ λ 맀κ°λ³μ μ€ μ΅μκ°μ λ°ννλ ν¨μ min(a, b)κ³Ό μ λ¬ λ 맀κ°λ³μ μ€ μ΅λκ°μ λ°ννλ ν¨μ max(a, b)λ₯Ό μ μνκ³
λ³μ μ΄κΈ°ν μ«μλ₯Ό λ³κ²½νλ©° μ΅μκ°κ³Ό μ΅λκ°μ ν¨μ νΈμΆμ ν΅ν΄ λ¦¬ν΄ λ°μ μ½μμ μΆλ ₯νλ ν μ€νΈλ₯Ό νμΈμ.
π© Example Output
μ΅μκ° : 10
μ΅λκ° : 20
// μ΅μκ°μ λ°ννλ ν¨μ
function min(a, b) {
a < b ? result = a : result = b
return result;
};
// μ΅λκ°μ λ°ννλ ν¨μ
function max(a, b) {
a > b ? result = a : result = b
return result;
};
// μ λ¬ λ°μ ν¨μμ μ λ¬ λ°μ κ°μ μ μ© μμΌμ£Όλ κ³ μ°¨ ν¨μ
function apply(func, a, b) {
return func(a, b);
}
const num1 = 10;
const num2 = 20;
console.log(`μ΅μκ° : ${apply(min, num1, num2)}`);
console.log(`μ΅λκ° : ${apply(max, num1, num2)}`);
πβ xμ nμ κ³±μ λ°νν΄μ£Όλ ν¨μ pow(x, n)λ₯Ό μ μνκ³ λ³μ μ΄κΈ°ν μ«μλ₯Ό λ³κ²½νλ©° ν¨μ νΈμΆνμ¬ μ½μλ‘ λ¦¬ν΄ λ°μ κ²°κ³Ό κ°μ μΆλ ₯νμΈμ.
π© Example Output
1024
λλ
-1μ μμ μ μμ΄μ΄μΌ ν©λλ€.
// xμ nμ κ³±μ λ°νν΄μ£Όλ ν¨μ
function pow(x, n) {
let result = 1;
if(n > 0) {
for(let i = 0; i < n; i++){
result = result * x;
}
} else {
result = `${n}μ μμ μ μμ΄μ΄μΌ ν©λλ€.`
}
return result;
}
const x = 2;
const n = 10;
console.log(pow(x, n));
πβ 2-1μμ ν¨μ μ μΈλ¬ΈμΌλ‘ μμ±ν min, max ν¨μλ₯Ό νμ΄ν ν¨μλ‘ λ³κ²½νμ¬ μμ± ν λ³μ μ΄κΈ°ν μ«μλ₯Ό λ³κ²½νλ©° μ΅μκ°κ³Ό μ΅λκ°μ ν¨μ νΈμΆμ ν΅ν΄ λ¦¬ν΄ λ°μ μ½μμ μΆλ ₯νλ ν μ€νΈλ₯Ό ν©λλ€.
- μΌν μ°μ°μλ₯Ό μ΄μ©νλ©΄ νλμ ꡬ문μΌλ‘ λ³κ²½ν μ μμ΅λλ€.
π© Example Output
μ΅μκ° : 10
μ΅λκ° : 20
// μ΅μκ°μ λ°ννλ ν¨μ
min = (a, b) => { a < b ? result = a : result = b; return result;};
// μ΅λκ°μ λ°ννλ ν¨μ
max = (a, b) => { a > b ? result = a : result = b; return result;};
// μ λ¬ λ°μ ν¨μμ μ λ¬ λ°μ κ°μ μ μ© μμΌμ£Όλ κ³ μ°¨ ν¨μ
apply = (func, a, b) => func(a, b);
const a = 10;
const b = 20;
console.log(`μ΅μκ° : ${apply(min, a, b)}`);
console.log(`μ΅λκ° : ${apply(max, a, b)}`);
πβ calculator λΌλ κ°μ²΄μ μλμ κ°μ λ©μλλ₯Ό μ μνκ³ λ©μλ μ€νμ ν μ€νΈ νμΈμ.
- sum λ©μλ : κ°μ²΄μ a, b λ κ°μ ν©μ λ°ν
- multi λ©μλ : κ°μ²΄μ a, b λ κ°μ κ³±μ λ°ν
π© Example Output
ν© : 30
κ³± : 200
const calculator = {
a : 10,
b : 20,
sum : function() {
return `ν© : ${this.a + this.b}`},
multi : function() {
return `κ³± : ${this.a * this.b}`}
};
console.log(calculator.sum());
console.log(calculator.multi());
πβ CalculatorλΌλ μμ±μ ν¨μμ μλμ κ°μ λ©μλλ₯Ό μ μνκ³ ν΄λΉ μμ±μλ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό λ§λ€μ΄ λ©μλ μ€νμ ν μ€νΈ νμΈμ
- sum λ©μλ : μ μ₯λ λ κ°μ ν©μ λ°ν
- multi λ©μλ : μ μ₯λ λ κ°μ κ³±μ λ°ν
π© Example Output
ν© : 30
κ³± : 200
function Calculator() {
this.a = 10;
this.b = 20;
this.sum = function() {
return `ν© : ${this.a + this.b}`},
this.multi = function() {
return `κ³± : ${this.a * this.b}`}
}
const sum = new Calculator().sum();
const multi = new Calculator().multi();
console.log(sum);
console.log(multi);
πβ μ λ¬ λ°μ λ¬Έμμ΄(str) μμ μλ κ°μ ',' κΈ°μ€μΌλ‘ λ°°μ΄λ‘ λΆλ¦¬νκ³ μ λ¬λ°μ λ¨μ΄(word)κ° μλ index μμΉλ₯Ό λ¬Έμμ΄λ‘ 리ν΄νλ ν¨μ findWord(str, word)λ₯Ό μμ±νμΈμ.
π© Example Output
κ³ μμ΄λ λ°°μ΄ index 10λ²μ§Έμ μμ΅λλ€.
μλμμΉλ λ°°μ΄μ μ‘΄μ¬νμ§ μμ΅λλ€.
κ²°κ³Όλ₯Ό νμΈν μ μμ΅λλ€.
const str = 'μ¬κ³Ό,λ°λλ,λΉκ·Ό,κΉλ°₯,μΉμ¦,λ‘λ³Άμ΄,νλ²κ±°,νν,μ¬μ΄λ€,μ½λΌ,κ³ μμ΄,κ°μμ§';
console.log(findWord(str, 'κ³ μμ΄'));
console.log(findWord(str, 'μλμμΉ'));
console.log(findWord(str));
function findWord(str, word) {
if (!word) {
return "κ²°κ³Όλ₯Ό νμΈν μ μμ΅λλ€.";
}
let wordArr = str.split(",");
let index = wordArr.indexOf(word);
if (index >= 0)
return `${word}λ λ°°μ΄ index ${index}λ²μ§Έμ μμ΅λλ€.`;
else
return `${word}λ λ°°μ΄μ μ‘΄μ¬νμ§ μμ΅λλ€.`;
}
πβ λ°°μ΄(arr)μ μμ μ€ aμ b μ¬μ΄μ μνμ§ μλ μμλ μμ ν΄μ£Όλ ν¨μ filterRange(arr, a, b)λ₯Ό μμ±νμΈμ.
π© Example Output
[5, 3, 1, 4]
function filterRange(arr, a, b) {
for(let i = 0; i < arr.length; i++) {
if(arr[i] < a || arr[i] > b) {
arr.splice(i, 1);
i--;
}
}
}
const arr = [5, 3, 8, 1, 10, 4];
filterRange(arr, 1, 5);
console.log(arr);
function filterRange(arr, a, b) {
arr.forEach((item, index) => {
if (item < a || item > b) {
arr.splice(index, 1);
}
});
}
const arr = [5, 3, 8, 1, 10, 4];
filterRange(arr, 1, 5); // 1κ³Ό 5 μ¬μ΄μ μμ§ μμ μμλ λͺ¨λ μ κ±°ν¨
console.log(arr); // [5, 3, 1, 4]
πβ μ μ λ μ«μκ° λ°°μ΄κ³Ό λ¬Έμκ° λ°°μ΄μ μ€λ¦μ°¨μ μ λ ¬, λ΄λ¦Όμ°¨μ μ λ ¬νμ¬ μΆλ ₯νμΈμ.
π© Example Output
[ 9, 20, 37, 54, 88, 100 ]
[ 100, 88, 54, 37, 20, 9 ]
[ 'hello', 'js', 'party', 'wow' ]
[ 'wow', 'party', 'js', 'hello' ]
let numbers = [20, 100, 37, 54, 88, 9];
let strings = ['wow', 'js', 'party', 'hello'];
console.log(numbers.sort((a, b) => a - b)); // μ«μ μ€λ¦μ°¨μ
console.log(numbers.sort((a, b) => b - a)); // μ«μ λ΄λ¦Όμ°¨μ
console.log(strings.sort()); // λ¬Έμμ΄ μ€λ¦μ°¨μ
console.log(strings.reverse()); // λ¬Έμμ΄ λ΄λ¦Όμ°¨μ
let numbers = [20, 100, 37, 54, 88, 9];
let strings = ['wow', 'js', 'party', 'hello'];
// μ«μ μ€λ¦μ°¨μ
numbers.sort((a, b) => a - b);
console.log(numbers);
// μ«μ λ΄λ¦Όμ°¨μ
numbers.sort((a, b) => b - a);
// numbers.reverse(); - μμ μ²λ¦¬λ κ°λ₯
console.log(numbers);
// λ¬Έμ μ€λ¦μ°¨μ
strings.sort();
console.log(strings);
// λ¬Έμ λ΄λ¦Όμ°¨μ
strings.sort(function (a, b) {
if(a > b) return -1;
if(a == b) return 0;
if(a < b) return 1;
});
// strings.reverse(); - μμ μ²λ¦¬λ κ°λ₯
console.log(strings);
πβ Student μμ±μ ν¨μλ₯Ό ν΅ν΄ μμ± λ 3λͺ μ νμ κ°μ²΄λ₯Ό studentList λ°°μ΄μ λ΄μμ΅λλ€. ν΄λΉ λ°°μ΄μ μ λ¬νμ¬ score λ΄λ¦Όμ°¨μ κΈ°μ€μΌλ‘ μ λ ¬ν΄μ£Όλ sortFromScore(arr) ν¨μ, ν΄λΉ λ°°μ΄μ μ λ¬νμ¬ lastNameκ³Ό firstNameμ ν©μ±νμ¬ name μμ±μΌλ‘ λ§λ€μ΄ λ°ννλ makeFullName ν¨μλ₯Ό μμ±νμΈμ.
π© Example Output
[
Student { firstName: 'κ΄μ', lastName: 'μ ', score: 80 },
Student { firstName: 'λ³΄κ³ ', lastName: 'μ₯', score: 70 },
Student { firstName: 'κΈΈλ', lastName: 'ν', score: 60 }
]
[
{ name: 'μ κ΄μ', score: 80 },
{ name: 'μ₯λ³΄κ³ ', score: 70 },
{ name: 'νκΈΈλ', score: 60 }
]
function Student(firstName, lastName, score){
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
const studentList = [
new Student('κΈΈλ', 'ν', 60),
new Student('λ³΄κ³ ', 'μ₯', 70),
new Student('κ΄μ', 'μ ', 80)
];
sortFromScore(studentList);
console.log(studentList);
console.log(makeFullName(studentList));
function sortFromScore (arr) {
arr.sort((a , b) => b.score - a.score);
}
function makeFullName(arr){
return arr.map(user => ({
name : user.lastName + user.firstName,
score : user.score })
);
}
πβ λ , μ, μΌμ μ λ¬νλ©΄ ν΄λΉ λ μ§μ μμΌμ 'μ', 'ν', 'μ', 'λͺ©', 'κΈ', 'ν ', 'μΌ' λ¬Έμμ΄λ‘ λ°νν΄μ£Όλ ν¨μ getWeekDayλ₯Ό λ§λμΈμ.
π© Example Output
ν
μ
console.log(getWeekDay(2022, 6, 21)); // 2022λ
6μ 21μΌ
console.log(getWeekDay(2022, 12, 12)); // 2022λ
12μ 12μΌ
function getWeekDay(year, month, date) {
const weekDayArr = new Array('μΌ', 'μ', 'ν', 'μ', 'λͺ©', 'κΈ', 'ν ');
const idx = new Date(`${year}-${month}-${date}`).getDay();
const weekDay = weekDayArr[idx];
return weekDay;
}
console.log(getWeekDay(2022, 6, 21)); // 2022λ
6μ 21μΌ
console.log(getWeekDay(2022, 12, 12)); // 2022λ
12μ 12μΌ
function getWeekDay(year, month, date) {
let when = new Date(year, month - 1, date);
let days = ['μΌ', 'μ', 'ν', 'μ', 'λͺ©', 'κΈ', 'ν '];
return days[when.getDay()];
}
πβ μ€λ νλ₯Έ μκ°μ μ΄ λ¨μλ‘ μλ €μ£Όλ getSecondsToday ν¨μμ μ€λ λ¨μ μκ°μ μ΄λ¨μλ‘ μλ €μ£Όλ getSecondsToTomorrow ν¨μλ₯Ό λ§λμΈμ.
π© Example Output
xxxxxμ΄λ νλ μ΅λλ€. μκ°μ μμ€ν μμλ€^^
xxxxxμ΄ λ°μ μ λ¨μμ΅λλ€. λ€μλ μ€μ§ μλ μ€λμ
λλ€^^
console.log(getSecondsToday());
console.log(getSecondsToTomorrow());
function getSecondsToday() {
let now = new Date();
let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
let diff = now - today;
return `${Math.round(diff / 1000)}μ΄λ νλ μ΅λλ€. μκ°μ μμ€ν μμλ€^^`;
}
function getSecondsToTomorrow() {
let now = new Date();
let tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1);
let diff = tomorrow - now;
return `${Math.round(diff / 1000)}μ΄ λ°μ μ λ¨μμ΅λλ€. λ€μλ μ€μ§ μλ μ€λμ
λλ€^^`;
}
πβ μκ·Όμ΄λ μλͺ κ³Όν μ°κ΅¬μμμ μΌμμ²΄κ° νΉμ ν ν¨ν΄μΈμ§λ₯Ό νμΈνλ μΌμ νκ³ μμ΅λλ€. μΌμ체λ μνλ²³ λλ¬Έμ (A, B, C, ..., Z)λ‘λ§ μ΄λ£¨μ΄μ§ λ¬Έμμ΄μ΄λ€. μκ·Όμ΄λ κ° μΌμμ²΄κ° λ€μκ³Ό κ°μ κ·μΉμ λ§μ‘±νλμ§ κ²μ¬ν΄μΌ ν©λλ€.
- λ¬Έμμ΄μ {A, B, C, D, E, F} μ€ 0κ° λλ 1κ°λ‘ μμν΄μΌ νλ€.
- κ·Έ λ€μμλ Aκ° νλ λλ κ·Έ μ΄μ μμ΄μΌ νλ€.
- κ·Έ λ€μμλ Fκ° νλ λλ κ·Έ μ΄μ μμ΄μΌ νλ€.
- κ·Έ λ€μμλ Cκ° νλ λλ κ·Έ μ΄μ μμ΄μΌ νλ€.
- κ·Έ λ€μμλ {A, B, C, D, E, F} μ€ 0κ° λλ 1κ°κ° μμΌλ©°, λ μ΄μμ λ¬Έμλ μμ΄μΌ νλ€.
λ¬Έμμ΄μ΄ μ£Όμ΄μ‘μ λ, μμ κ·μΉμ λ§μ‘±νλμ§ κ΅¬νλ νλ‘κ·Έλ¨μ μμ±νμΈμ.
π© Example Output
Good
Infected!
Infected!
Infected!
Infected!
Infected!
Good
Good
Good
Good
const words = [
'15',
'AFC',
'AAFC',
'AAAFFCC',
'AAFCC',
'BAFC',
'QWEDFGHJMNB',
'DFAFCB',
'ABCDEFC',
'DADC'
];
const regexp = /^[A-F]?A+F+C+[A-F]?$/;
words.forEach(item => {
if(item.match(regexp))
console.log('Infected!');
else
console.log('Good');
});
πβ arr λ°°μ΄μλ μ«μ λ€μ λ¨μ λ¬Έμκ° λΆκ±°λ μ«μ μμ νΉμ λ¬Έμκ° 1κ° λΆλ ννμ λ¬Έμμ΄λ€μ΄ λ΄κ²¨μμ΅λλ€. ν΄λΉ κ°λ€μ μ λλ λ€μ μμΉν λ¬Έμλ₯Ό μ κ±°νκ³ μ«μλ‘ νλ³νν κ°λ€μ΄ λ΄κΈ΄ μλ‘μ΄ λ°°μ΄μ λ°ννλ removeChar ν¨μλ₯Ό μμ±νμΈμ.
π© Example Output
[ 1080, 100, 200, 60, 6, -15 ]
let arr = ["1080px", "$100", "+200", "60kg", "6m", "-15"];
console.log(removeChar(arr));
function removeChar(arr){
return arr.map(item => parseInt(item) || +item.slice(1));
}
πβ μμΌλ‘ μ½μ΄λ κ±°κΎΈλ‘ μ½μ΄λ λκ°μ λ¬ΈμμΈμ§ νλ³νλ palindrom ν¨μλ₯Ό λ§λμΈμ.
π© Example Output
true
true
true
true
true
false
const list = ['κΈ°λ¬κΈ°', 'ν λ§ν ', 'μ€μμ€', 'μΈλμΈ', 'λ³λ₯λ³', 'μ°λ³λ¦Ό'];
list.forEach(item => console.log(palindrom(item)));
function palindrom(str) {
return str === str.split('').reverse().join('')
}
πβ input type="color" νκ·Έλ‘ μμ μ ν ν λ³κ²½ λ²νΌμ ν΄λ¦νλ©΄ νλ¨ divμ μμμ΄ λ³κ²½λλλ‘ λ§λμΈμ.
π© Example Output
[μ΄λ―Έμ§]
<div id="div1" class="area"></div>
<input type="color" id="selectColor">
<button onclick="colorChange();">λ³κ²½</button>
function colorChange() {
const $area = document.getElementById('div1');
const $color = document.getElementById('selectColor');
$area.style.backgroundColor = $color.value;
};
πβ λ²νΌ ν΄λ¦ μ νλ¨ divμ ν¬κΈ°κ° μ‘°μ λλλ‘ λ§λμΈμ.
π© Example Output
[μ΄λ―Έμ§]
<div id="div2" class="area"></div>
<button onclick="halfSize();">50x50</button>
<button onclick="defaultSize();">μλ³Έ(100x100)</button>
<button onclick="doubleSize();">200x200</button>
function halfSize() {
const $area = document.getElementById('div2');
$area.style.width = '50px';
$area.style.height = '50px';
};
function defaultSize() {
const $area = document.getElementById('div2');
$area.style.width = '100px';
$area.style.height = '100px';
}
function doubleSize() {
const $area = document.getElementById('div2');
$area.style.width = '200px';
$area.style.height = '200px';
}
πβ 첫 λ²μ§Έ κ°κ³Ό λ λ²μ§Έ κ°μ μ λ ₯ ν μ°μ°μ λ²νΌ ν΄λ¦ μ λμνλ calculator ν¨μλ₯Ό λ§λμΈμ. ν¨μμ 맀κ°λ³μλ‘ μ°μ°μ λ²νΌμ΄ κ°μ§κ³ μλ value κ°μ΄ μ λ¬λ©λλ€.
π© Example Output
[μ΄λ―Έμ§]
첫 λ²μ§Έ κ° : <input id="num1">
λ λ²μ§Έ κ° : <input id="num2">
<input type="button" value="+" onclick="calculator(this.value);">
<input type="button" value="-" onclick="calculator(this.value);">
<input type="button" value="x" onclick="calculator(this.value);">
<input type="button" value="/" onclick="calculator(this.value);">
<input type="button" value="%" onclick="calculator(this.value);">
κ³μ° κ²°κ³Ό : <span id="calc"></span>
function calculator(value) {
const $calc = document.getElementById('calc');
switch(value){
case '+' : result = Number(num1.value) + Number(num2.value); break;
case '-' : result = Number(num1.value) - Number(num2.value); break;
case 'x' : result = Number(num1.value) * Number(num2.value); break;
case '/' : result = Number(num1.value) / Number(num2.value); break;
case '%' : result = Number(num1.value) % Number(num2.value); break;
}
$calc.innerHTML = result;
}
function calculator(value) {
let su1 = Number(document.getElementById("num1").value);
let su2 = Number(document.getElementById("num2").value);
let result = 0;
switch (value) {
case "+": result = su1 + su2; break;
case "-": result = su1 - su2; break;
case "x": result = su1 * su2; break;
case "/": result = su1 / su2; break;
case "%": result = su1 % su2; break;
}
document.getElementById("calc").textContent = result;
}
πβ λ²νΌ ν΄λ¦ μ μλ ul νκ·Έ(id:list)μ νμ νκ·Έ(li)λ₯Ό λͺ¨λ μμ νλ deleteList ν¨μλ₯Ό λ§λμΈμ.
π© Example Output
[μ΄λ―Έμ§]
<button onclick="deleteList();">λͺ©λ‘ μμ </button>
<ul id="list">
<li>μλ₯</li>
<li>μ‘ν</li>
<li>κ°μ </li>
<li>κ°κ΅¬</li>
<li>μν</li>
<li>λ μ </li>
<li>μ¬κ°</li>
<li>λμ</li>
</ul>
function deleteList() {
const $list = document.getElementById('list');
$list.innerHTML = "";
}
πβ λ²νΌ ν΄λ¦ μ "μΆκ°ν λͺ©λ‘μ μ λ ₯νμΈμ"λΌλ prompt μ°½μ λμ°κ³ μ λ ₯ λ°μ κ°μ λ΄λΆ ν μ€νΈλ‘ νμ¬ λμ μΌλ‘ li μμ μμ± ν μ ul νκ·Έ(id:list)μ νμ νκ·Έλ‘ μΆκ°ν©λλ€.
μ λ ₯νμ§ μκ³ μ·¨μ νμ μμλ μΆκ°νμ§ μμ΅λλ€.
π© Example Output
[μ΄λ―Έμ§]
<button onclick="createList();">λͺ©λ‘ μμ±</button>
function createList(){
let content = prompt('μΆκ°ν λͺ©λ‘μ μ
λ ₯νμΈμ');
const $list = document.getElementById('list');
const $li = document.createElement('li');
if(!content==""){
$li.textContent = content;
$list.appendChild($li);
}
};
πβ μΌμͺ½μΌλ‘ λ²νΌ ν΄λ¦ μ νλ¨μ first, second, third divκ° ν μΉΈμ© μΌμͺ½μΌλ‘ μ΄λνκ² νλ left ν¨μλ₯Ό μμ±νμΈμ.
- ex)
first - second - third μΈ μνμμ left 1λ² ν΄λ¦νλ©΄ second - third - firstλ‘ λ³κ²½
left ν λ² λ ν΄λ¦νλ©΄ third - first - second λ‘ λ³κ²½
π© Example Output
[μ΄λ―Έμ§]
<button onclick="left();">μΌμͺ½μΌλ‘</button>
<div id="area">
<div class="first">first</div>
<div class="second">second</div>
<div class="third">third</div>
</div>
function left(){
const $area = document.getElementById("area");
$area.appendChild($area.firstElementChild);
}
πβ μΆκ°νκΈ° λ²νΌμ ν λ² ν΄λ¦νλ©΄ κ°λ‘, μΈλ‘ 100px, 1pxμ μ§μ κ²μ ν λ리λ₯Ό κ°μ§ λΉ¨κ°μ divλ₯Ό area1 λ΄λΆμ μΆκ°νκ³ ν λ² λ ν΄λ¦νλ©΄ κ°μ μ€νμΌμ λ Έλμ divλ₯Ό area1 λ΄λΆμ μΆκ°νμΈμ. μμ νκΈ° λ²νΌμ λλ₯΄λ©΄ λ§μ§λ§μ μΆκ° λ divλ₯Ό μμ ν©λλ€. divλ μμ§μ΄ μλ μνμΌλ‘ μΆκ°λμ΄μΌ νλ©° μμ λ div μ΄νμ μΆκ° λ λλ λΉ¨κ°μκ³Ό λ Έλμ divκ° λ²κ°μκ°λ©° μΆκ° λμ΄μΌ ν©λλ€.
π© Example Output
[μ΄λ―Έμ§]
<button id="add">μΆκ°νκΈ°</button>
<button id="remove">μμ νκΈ°</button>
<div id="area1" class="area"></div>
document.getElementById("add").addEventListener('click', function () {
const $area1 = document.getElementById("area1");
const $newDiv = document.createElement("div");
$newDiv.style.width = "100px";
$newDiv.style.height = "100px";
$newDiv.style.border = "1px solid black";
$newDiv.style.display = "inline-block";
if (!$area1.lastElementChild || $area1.lastElementChild.style.backgroundColor != "red") {
$newDiv.style.backgroundColor = "red";
} else {
$newDiv.style.backgroundColor = "yellow";
}
$area1.appendChild($newDiv);
});
document.getElementById("remove").addEventListener('click', function () {
const $area1 = document.getElementById("area1");
if ($area1.lastElementChild)
$area1.removeChild($area1.lastElementChild);
});
πβ μμΉκΈμ‘, μμΉκΈ°κ°, μ°μ΄μμ¨ κ°μ΄ λ³κ²½ λ λλ§λ€ κ³μ° λ κ²°κ³Όλ₯Ό μκΈκ³Ό λ§κΈ°μ§κΈκΈμ‘μΌλ‘ νκΈ°ν©λλ€.
π© Example Output
[μ΄λ―Έμ§]
<form name="calculator">
<table>
<tr>
<td>μμΉκΈμ‘</td>
<td>
<input name="money" type="number" value="10000" required>
</td>
</tr>
<tr>
<td>μμΉκΈ°κ°</td>
<td>
<select name="months">
<option value="3">3 (μ΅λ¨ μμΉκΈ°κ°)</option>
<option value="6">6 (λ°λ
)</option>
<option value="12" selected>12 (1λ
)</option>
<option value="18">18 (1.5λ
)</option>
<option value="24">24 (2λ
)</option>
<option value="30">30 (2.5λ
)</option>
<option value="36">36 (3λ
)</option>
<option value="60">60 (5λ
)</option>
</select>
</td>
</tr>
<tr>
<td>μ°μ΄μμ¨</td>
<td>
<input name="interest" type="number" value="5" required>
</td>
</tr>
</table>
</form>
μκΈ : <span id="money-before">10000</span>
λ§κΈ°μ§κΈκΈμ‘ : <span id="money-after">10500</span>
// νΌ μ»κΈ°
const $form = document.forms.calculator;
// 3κ° μ
λ ₯ μμμ κ³μ°μ© ν¨μ μ΄λ²€νΈ κ±ΈκΈ°
$form.money.oninput = calculate;
$form.months.oninput = calculate;
$form.interest.oninput = calculate;
function calculate() {
const $form = document.forms.calculator;
// μμΉκΈμ‘ : λ¬Έμ -> μ«μλ‘ νλ³ν
let initial = +$form.money.value;
if (!initial) return;
// μμΉ κΈ°κ° : 1λ
κΈ°μ€μ μν΄ 12λ‘ λλ
let years = $form.months.value / 12;
if (!years) return;
// μ°μ΄μμ¨ : νΌμΌνΈμ΄λ―λ‘ 0.01μ κ³±ν¨
let interest = $form.interest.value * 0.01;
if (!interest) return;
// λ°μ¬λ¦Ό μ²λ¦¬ (μμΉκΈμ‘ * (1 + μ°μ΄μμ¨ * μμΉ κΈ°κ°(λ
)))
let result = Math.round(initial * (1 + interest * years));
// μκΈκ³Ό λ§κΈ°μ§κΈκΈμ‘ μ
λ ₯
document.getElementById('money-before').textContent = $form.money.value;
document.getElementById('money-after').textContent = result;
}
// λ¬Έμ λ‘λ ν 1λ² μλ μ°μ°ν΄μ κ° μ€μ
calculate();
πβ divλ₯Ό ν΄λ¦νλ©΄ textareaλ‘ λ³κ²½μ΄ λκ³ κ·Έ μμμ κ°μ μ λ ₯ν λ€ Enterλ₯Ό λλ₯΄κ±°λ blur μ΄λ²€νΈκ° λ°μνλ©΄ κ²°κ³Όλ₯Ό div μμ μ μ₯ν©λλ€.
- HTMLμ μ μΌλ©΄ νκ·Έλ‘ κΈ°λ₯
- μ°Έκ³ : textarea νκ·Έλ₯Ό λ§λ€λ©΄ editμ΄λΌλ ν΄λμ€λͺ μ λΆμ¬ (CSS μ€μ μ΄ μμ)
- node.replaceWith(λ Έλλ λ¬Έμμ΄) λ©μλλ₯Ό μ¬μ©ν΄λ³΄κΈ°
π© Example Output
[μ΄λ―Έμ§]
<div id="view" class="view">μ¬κΈ°λ₯Ό ν΄λ¦ν΄μ λ΄μ©μ μ
λ ₯νμΈμ.</div>
let $area = null;
let $view = document.getElementById('view');
$view.onclick = editStart;
function editStart() {
$area = document.createElement('textarea');
$area.className = 'edit';
$area.value = $view.innerHTML;
$area.onkeydown = function (event) {
if (event.key == 'Enter') {
this.blur();
}
};
$area.onblur = editEnd;
$view.replaceWith($area);
$area.focus();
}
function editEnd() {
$view.innerHTML = $area.value;
$area.replaceWith($view);
}
πβ μμ λ²νΌμ λλ₯΄λ©΄ hh:mm:ssμ 곡κ°μ νμ¬ μκ°μ΄ 보μ¬μ§κ³ λ©μΆ€ λ²νΌμ λλ₯΄λ©΄ λ€μ hh:mm:ssλ‘ λ³΄μ¬μ§κ² νλ start, stop ν¨μλ₯Ό μμ±νμΈμ. λ¨, νμ리 μ«μλ μμ 0μ λΆμ¬μ λμλ¦¬λ‘ ννν©λλ€.
π© Example Output
[μ΄λ―Έμ§]
<button onclick="start();">μμ</button>
<button onclick="stop();">λ©μΆ€</button>
<div id="clock">
<span class="hour">hh</span>:<span class="min">mm</span>:<span class="sec">ss</span>
</div>
const $clock = document.getElementById('clock');
const $hour = document.getElementsByClassName('hour');
const $min = document.getElementsByClassName('min');
const $sec = document.getElementsByClassName('sec');
function start(){
const date = new Date();
const hour = date.getHours();
const min = date.getMinutes();
const sec = date.getSeconds();
$clock.innerHTML = `${$hour.innerHTML = hour < 10 ? `0${hour}` : hour}:${$min.innerHTML = min < 10 ? `0${min}` : min}:${$sec.innerHTML = sec < 10 ? `0${sec}` : sec}`
const timerId = setInterval(start, 1000);
}
function stop() {
location.reload();
}
let timer;
const clock = document.querySelector("#clock");
function start(){
if(!timer){
timer = setInterval(update, 1000);
update();
}
}
function stop(){
clearInterval(timer);
timer = null;
clock.children[0].textContent = 'hh';
clock.children[1].textContent = 'mm';
clock.children[2].textContent = 'ss';
}
function update(){
let date = new Date();
let hour = date.getHours();
let min = date.getMinutes();
let sec = date.getSeconds();
clock.children[0].textContent = hour < 10 ? '0' + hour : hour;
clock.children[1].textContent = min < 10 ? '0' + min : min;
clock.children[2].textContent = sec < 10 ? '0' + sec : sec;
}
πβ μλμ 쑰건μ λ§λ sum ν¨μλ₯Ό μμ±ν©λλ€. promptλ‘ μ¬μ©μμκ² μ«μ μ λ ₯μ λ°λ³΅ μμ²ν ν, μ λ ₯λ°μ κ°λ€μ λ°°μ΄μ μ μ₯ν©λλ€. μ«μκ° μλ κ° νΉμ λΉ λ¬Έμμ΄μ μ λ ₯νκ±°λ μ·¨μ λ²νΌμ λλ₯΄λ©΄ prompt μ°½μ λ°λ³΅μ λ©μΆκ³ λ°°μ΄μ λ΄κΈ΄ μ«μμ ν©κ³λ₯Ό λ°νν©λλ€.
(λ¨, μ«μ 0μ μ ν¨ν μ λ ₯μΌλ‘ κ°μ£Όν©λλ€.)
π© Example Output
[μ΄λ―Έμ§]
function sum() {
let numbers = [];
while (true) {
let value = prompt("μ«μλ₯Ό μ
λ ₯ν΄ μ£ΌμΈμ.", 0);
if (!value || isNaN(value)) break;
numbers.push(Number(value));
}
let sum = 0;
for (let key in numbers) {
sum += numbers[key];
}
return sum;
}
console.log("ν©κ³ : " + sum());
πβ κ° μΆκ° λ²νΌμ λλ μ λ λ¨λ promptμ μ λ ₯ λ κ°μ λ°°μ΄μ μμλλ‘ μ μ₯ν©λλ€. λ¨, μ·¨μ λ° κ° μ λ ₯ μμ΄ νμΈμ λλ μ κ²½μ°λ μ μ₯νμ§ μμ΅λλ€. κ° μΆλ ₯ λ²νΌμ λλ₯΄λ©΄ μ§κΈκΉμ§ μ λ ₯ λ κ°μ alertλ‘ μΆλ ₯ν©λλ€. λ¨, μμ§ μ λ ₯ λ κ°μ΄ μλ μνμμ λ²νΌμ΄ λ리면 'κ°μ΄ λ¨Όμ μ λ ₯ λμ΄μΌ ν©λλ€.'λ₯Ό μΆλ ₯ν©λλ€. κ° μμ λ²νΌμ λλ₯΄λ©΄ μ§κΈκΉμ§ μ λ ₯ λμλ λ°°μ΄μ κ°μ λͺ¨λ μ κ±°ν λ€ 'κ°μ΄ λͺ¨λ μμ λμμ΅λλ€.'λ₯Ό μΆλ ₯ν©λλ€.
π© Example Output
[μ΄λ―Έμ§]
<button onclick="input(prompt('κ°μ μ
λ ₯νμΈμ'));">κ° μΆκ°</button>
<button onclick="print();">κ° μΆλ ₯</button>
<button onclick="reset();">κ° μμ </button>
let data = [];
function input(value) {
if (value)
data.push(value);
}
function print() {
if (data.length != 0)
alert(data);
else
alert('κ°μ΄ λ¨Όμ μ
λ ₯ λμ΄μΌ ν©λλ€.');
}
function reset() {
data.length = 0;
alert('κ°μ΄ λͺ¨λ μμ λμμ΅λλ€.');
}