๐ "์์๋๋ฅผ ๊ทธ๋ฆฌ๋ ๊ฒ ์ฐ์ ์ด๋ค." ์ฒ์ ๋ฃ๋๋น...
(์ฒจ์ธ : ์์๋ ๋ํ ๊ฐ๋ฐ์ ์ ํํ ๊ณํ์ ์ข
๋ฅ ์ค ํ๋์ด๊ณ , ๋ณธ๊ฒฉ ๊ฐ๋ฐ ์ ์ ์ค๊ณ ๋ฌธ์๋ฅผ ์์ฑํด์ ๋๋ฃ์ ๋ฆฌ๋ทฐ๋ฅผ ํ๋ฉด ์ข๋ค.
'์ฝ๋ฉ์ ๊ทธ๋ฅ ์ง๋ฉด ๋๋๊น ๊ฐ๋ฐ๋ณด๋ค ์ค๊ณ๊ฐ ๋ ์ค์ํ๋ค'๊ณ ํ๊ธฐ๋ ํ๋๋ฐ ๋... ์ฝ๋ฉ๋ ์ ๋ชป ์ง๋๋ฐ์...)
์ฌ๋์ ํ๋์ ๊ณ ๋ คํ์ฌ ์์ฑํ ์์๋๋ฅผ 'JS์ ๋ง๊ฒ' ์์ ํ๊ณ ์ชผ๊ฐ์ผ ํ๋ค.
๊ณ์ฐ๊ธฐ๋ ๐ฅ + โญ๏ธ + ๐ด (์ฒซ ๋ฒ์งธ๋ก ์ ๋ ฅ๋ ์ซ์ + ์ฐ์ฐ์ + ๋ ๋ฒ์งธ๋ก ์ ๋ ฅ๋ ์ซ์) ๋ก ์ด๋ฃจ์ด์ง๋๊น
// โ
let firstNum = '';
let operator = '';
let secondNum = '';
const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');
// โ ๊ณ ์
// โ STEP 1
document.querySelector('#num-0').addEventListener('click', () => {
if (operator) { // ์ฐ์ฐ์๊ฐ ์๋ค๋ฉด
secondNum += '0'; // ๋ ๋ฒ์งธ ์ซ์์ 0์ ๋ฃ๊ณ ~
} else {
firstNum += '0';
}
$result.value += '0'}); // JS์์ ๋ณ์๋ก ๋ถ๋ฌ์์ผ๋ฉด HTML์ ๊ทธ๋ ค๋ด๋ ๊ฒ ์์ง ๋ง๊ธฐ
// โ ์ด๋ ๊ฒ ์ซ์๋ฅผ ํ๋ํ๋ ์ ์ผ๋ฉด 0๋ถํฐ 9๊น์ง (#num-0๋ถํฐ #num0=-9๊น์ง)10๋ฒ์ ์ ์ด์ค์ผ ํ๋๋ฐ, ์ด๋ ๊ฒ ํจ์์์ ์ค๋ณต๋๋ ๋ถ๋ถ์ ํ๋๋ก ๋ง๋ค ๋ ๋ค๋ฅธ ๋ถ๋ถ์ด ์๋ค๋ฉด ๊ทธ๊ฑด ๋งค๊ฐ๋ณ์๋ก ๋นผ์ค๋ค. ์ด ๊ฒฝ์ฐ๋ 'number'
// ----------------------------------------------------------------
// โ STEP 2
document.querySelector('#num-0').addEventListener('click', (number) => {
if (operator) {
secondNum += number;
} else {
firstNum += number;
}
$result.value += number});
// โ ์ด๋ ๊ฒ ํ๋ฉด number ๋๋ถ์ ๋์ผํ ์ฝ๋๊ฐ ๋ฐ๋ณต๋๋ฏ๋ก ๋ฐ๋ณต๋๋ ๋ถ๋ถ์ ๋ณ์๋ก ์ ์ฅํ๋ค. ์ด ๊ฒฝ์ฐ๋ const onClickNumber
// ----------------------------------------------------------------
// โ STEP 3
const onClickNumber = (number) => {
if (operator) {
secondNum += number;
} else {
firstNum += number;
}
$result.value += number};
document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ ์ด๋ ๊ฒ ํ๋ฉด const์ ๊ฒฐ๊ณผ ๊ฐ์ด undefined์ฌ์ ์๋ onClickNumber ์๋ฆฌ์ undefined๊ฐ ๋ค์ด๊ฐ๋ฏ๋ก return๋ฌธ ์ถ๊ฐํ๋ค.
// ----------------------------------------------------------------
// โ STEP 4
const onClickNumber = (number) => {
if (operator) {
secondNum += number;
} else {
firstNum += number;
}
$result.value += number
return () => {
};
};
document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ ์ด๋ ๊ฒ ํ๋ฉด return๋ฌธ ๋๋ถ์ undefined๋ ๋ฉดํ์ง๋ง ์ฌ์ ํ ๋น ๊ฐ์ด onClickNumber ์๋ฆฌ์ ๋ค์ด๊ฐ๊ฒ ๋๋ฏ๋ก ๊ฐ์ด ํ์ํ if๋ฌธ์ return๋ฌธ ์์ผ๋ก ๋ฃ๋๋ค.
// ----------------------------------------------------------------
// โ STEP 5
const onClickNumber = (number) => {
return () => {
if (operator) {
secondNum += number;
} else {
firstNum += number;
}
$result.value += number
};
};
document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ ์ด๋ ํ์ดํ ํจ์์์ {}์ return์ด ํจ๊ป ์์ผ๋ฉด ์๋ต์ด ๊ฐ๋ฅํ๋ฏ๋ก
// ----------------------------------------------------------------
// โ STEP 6
const onClickNumber = (number) => () => {
if (operator) {
secondNum += number;
} else {
firstNum += number;
}
$result.value += number
};
document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ ํจ์๊ฐ ํจ์๋ฅผ returnํ๋ ๊ณ ์ฐจํจ์ (high order function) ์์ฑ
// ----------------------------------------------------------------
// โ STEP 7
const onClickNumber = (number) => (event) => {
if (operator) {
secondNum += number;
} else {
firstNum += number;
}
$result.value += number
};
document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ onClickNumber๊ฐ์ event๋ฅผ ์ฐ๋ฉด ๋ธ๋ผ์ฐ์ ๊ฐ event๋ฅผ ํธ์ถํ๋ฉด์ event๋ผ๋ ์ธ์๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ๋ค. ๋ธ๋ผ์ฐ์ ๊ฐ ์๋์ผ๋ก ์๋ ฅํ๋ฏ๋ก STEP 6์์ ๋ฉ์ถ์ด๋ ๋๋ค.
// ===============================================================
// ๐ ๊ณ ์ฐจํจ์๋ฅผ ํ์ฉํ ํจ์ ์ค๋ณต ์ ๊ฑฐ
let firstNum = '';
let operator = '';
let secondNum = '';
const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');
const onClickNumber = (number) => (event) => {
if (operator) {
secondNum += number;
} else {
firstNum += number;
}
$result.value += number
};
document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('1'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('2'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('3'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('4'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('5'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('6'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('7'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('8'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('9'));
// ๐
// ๐
let firstNum = '';
let operator = '';
let secondNum = '';
const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');
const onClickNumber = (event) => {
if (operator) {
secondNum += event.target.textContent;
} else {
firstNum += event.target.textContent;
}
$result.value += event.target.textContent;
};
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
// ๐
1+2๋ฅผ ๋๋ฅด๋ฉด 12๊ฐ ๋๋ ์ํฉ์ ๋ฐ๊พธ๊ธฐ ์ํด onClickNumber์ if๋ฌธ ์ถ๊ฐ
"์ฐ์ฐ์๋ ์๋๋ฐ ๋ ๋ฒ์งธ ์ซ์๊ฐ ์์ผ๋ฉด ๊ฒฐ๊ณผ์ฐฝ ํ๋ฉด์ ๋น์๋ผ. (์๋ํ๋ฉด ๋ ๋ฒ์งธ ์ซ์ ์ง๊ธ ์
๋ ฅํ ๊ฑฐ๋๊น.)"
const onClickNumber = (event) => {
if (operator) {
if (!second) {
$result.value = '';
}
secondNum += event.target.textContent;
} else {
firstNum += event.target.textContent;
}
$result.value += event.target.textContent;
};
// โ STEP 1. if๋ฌธ ๋ค์์ ๋์ค๋ ๊ณตํต๋ ์ ์ฐจ๋ฅผ ๊ฐ ๋ถ๊ธฐ์ ๋ด๋ถ์ ๋ฃ๋๋ค.
const onClickNumber (event) => {
if (operator) {
if (!second) {
$result.value = '';
}
secondNum += event.target.textContent;
$result.value += event.target.textContent;
} else {
fistNum += event.target.textContent;
$result.value += event.target.textContent;
}
};
// โ
// ----------------------------------------------------------------
// โ STEP 2. ๋ถ๊ธฐ์ ์์ ์งง์ ์ ์ฐจ๋ถํฐ ์คํํ๋๋ก if๋ฌธ์ ์์ฑํ๋ค. ์ฆ ๋ ์งง์ else ๋ค code๊ฐ ์๋ก ๊ฐ๊ฒ, ๊ธด code๊ฐ ์๋๋ก ๊ฐ๊ฒ ๋ณ๊ฒฝํ๋ค. ์ด๋ ๊ฒ ๋ณ๊ฒฝํ ๋ ์ธ์์ ๋๋ํ๋ฅผ ๋ถ์ด๋ฉด ๋๋ค. (์ด ๊ฒฝ์ฐ๋ operator์ ๋๋ํ๋ฅผ ๋ถ์ธ๋ค.)
const onClickNumber (event) => {
if (!operator) {
fistNum += event.target.textContent;
$result.value += event.target.textContent;
} else {
if (!second) {
$result.value += '';
}
secondNum += event.target.textContent;
$result.value += event.target.textContent;
}
};
// โ
// ----------------------------------------------------------------
// โ STEP 3. ์งง์ ์ ์ฐจ๊ฐ ๋๋ ๊ตฌ๊ฐ์ return (ํจ์ ๋ด๋ถ)๋ break (for๋ฌธ ๋ด๋ถ)๋ก ์ค๋จํ๋ค.
const onClickNumber (event) => {
if (!operator) {
fistNum += event.target.textContent;
$result.value += event.target.textContent;
return;
} else {
if (!second) {
$result.value += '';
}
secondNum += event.target.textContent;
$result.value += event.target.textContent;
}
};
//
// ----------------------------------------------------------------
// โ STEP 4. if๋ฅผ return์ผ๋ก ์ค๋จํ๋ฉด else๊ฐ ํ์ ์๋ค.
const onClickNumber (event) => {
if (!operator) {
fistNum += event.target.textContent;
$result.value += event.target.textContent;
return;
}
if (!second) {
$result.value += '';
}
secondNum += event.target.textContent;
$result.value += event.target.textContent;
}
};
// โ
// ===============================================================
// ๐ ์ต์ข
code
let firstNum = '';
let operator = '';
let secondNum = '';
const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');
const onClickNumber = (event) => {
if (!operator) {
firstNum += event.target.textContent;
$result.value += event.target.textContent;
return;
}
if (!secondNum) {
$result.value = '';
}
secondNum += event.target.textContent;
$result.value += event.target.textContent;
};
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-1').addEventListener('click', onClickNumber);
document.querySelector('#num-2').addEventListener('click', onClickNumber);
document.querySelector('#num-3').addEventListener('click', onClickNumber);
document.querySelector('#num-4').addEventListener('click', onClickNumber);
document.querySelector('#num-5').addEventListener('click', onClickNumber);
document.querySelector('#num-6').addEventListener('click', onClickNumber);
document.querySelector('#num-7').addEventListener('click', onClickNumber);
document.querySelector('#num-8').addEventListener('click', onClickNumber);
document.querySelector('#num-9').addEventListener('click', onClickNumber);
// ๐
// STEP 1.
document.querySelector('#plus').addEventListener('click', () => {
if (firstNum) {
operator = '+'
$operator.value = '+';
} else {
alert('์ซ์๋ฅผ ๋จผ์ ์
๋ ฅํ์ธ์ :>');
}
});
// ----------------------------------------------------------------
// STEP 2.
document.querySelector('#plus').addEventListener('click', (op) => {
if (firstNum) {
operator = op;
$operator.value = op;
} else {
alert ('์ซ์๋ฅผ ๋จผ์ ์
๋ ฅํ์ธ์ :>');
}
})
// ----------------------------------------------------------------
// STEP 3.
const onClickOperator = (op) => {
if (firstNum) {
operator = op;
$operator.value = op;
} else {
alert ('์ซ์๋ฅผ ๋จผ์ ์
๋ ฅํ์ธ์ :>');
}
}
document.querySelector('#plus')addEventListener('click,' onClickOperator('+'));
// ----------------------------------------------------------------
// STEP 4.
const onClickOperator = (op) => {
if (firstNum) {
operator = op;
$operator.value = op;
} else {
alert ('์ซ์๋ฅผ ๋จผ์ ์
๋ ฅํ์ธ์ :>');
}
return () => {
};
}
document.querySelector('#plus').addEventListener('click',onClickOperator('+'));
// ----------------------------------------------------------------
// STEP 5.
const onClickOperator = (op) => {
return () => {
if (firstNum) {
operator = op;
$operator.value = op;
} else {
alert ('์ซ์๋ฅผ ๋จผ์ ์
๋ ฅํ์ธ์ :>');
};
};
};
document.querySelector('#plus').addEventListener('click',onClickOperator('+'));
// ----------------------------------------------------------------
// STEP 6.
const onClickOperator = (op) => () => {
if (firstNum) {
operator = op;
$operator.value = op;
} else {
alert ('์ซ์๋ฅผ ๋จผ์ ์
๋ ฅํ์ธ์ :>');
};
};
document.querySelector('#plus').addEventListener('click' onClickOperator('+'));
// ===============================================================
// ๐
const onClickOperator = (op) => {
return () => {
if (firstNum) {
operator = op;
$operator.value = op;
} else {
alert ('์ซ์๋ฅผ ๋จผ์ ์
๋ ฅํ์ธ์ :>');
};
};
};
document.querySelector('#plus').addEventListener('click',onClickOperator('+'));
document.querySelector('#minus').addEventListener('click', onClickOperator('-'));
document.querySelector('#divide').addEventListener('click', onClickOperator('/'));
document.querySelector('#multiply').addEventListener('click', onClickOperator('*'));
// ๐