์ง์ง ๋๋ฌด ํ๋ค์๊ณ ์์ฃผ ๋ง์ด ์ ๋จน์๋ค..
const calculator = document.querySelector('.calculator'); // calculator ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
const buttons = calculator.querySelector('.calculator__buttons'); // calculator__keys ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
const firstOperend = document.querySelector('.calculator__operend--left'); // calculator__operend--left ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
const operator = document.querySelector('.calculator__operator'); // calculator__operator ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
const secondOperend = document.querySelector('.calculator__operend--right'); // calculator__operend--right ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
const calculatedResult = document.querySelector('.calculator__result'); // calculator__result ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
function calculate(n1, operator, n2) {
let result = 0;
n1 = +n1;
n2 = +n2;
// TODO : n1๊ณผ n2๋ฅผ operator์ ๋ฐ๋ผ ๊ณ์ฐํ๋ ํจ์๋ฅผ ๋ง๋์ธ์.
// ex) ์
๋ ฅ๊ฐ์ด n1 : '1', operator : '+', n2 : '2' ์ธ ๊ฒฝ์ฐ, 3์ด ๋ฆฌํด๋ฉ๋๋ค.
if (operator === '+'){
result = n1 + n2
} else if (operator === '-'){
result = n1 - n2
} else if (operator === '*'){
result = n1 * n2
} else if (operator === '/'){
result = n1 / n2
}
return String(result);
}
buttons.addEventListener('click', function (event) {
// ๋ฒํผ์ ๋๋ ์ ๋ ์๋ํ๋ ํจ์์
๋๋ค.
const target = event.target; // ํด๋ฆญ๋ HTML ์๋ฆฌ๋จผํธ์ ์ ๋ณด๊ฐ ์ ์ฅ๋์ด ์์ต๋๋ค.
const action = target.classList[0]; // ํด๋ฆญ๋ HTML ์๋ฆฌ๋จผํธ์ ํด๋ ์ค ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
const buttonContent = target.textContent; // ํด๋ฆญ๋ HTML ์๋ฆฌ๋จผํธ์ ํ
์คํธ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
// ! ์ ์ฝ๋(Line 19 - 21)๋ ์์ ํ์ง ๋ง์ธ์.
if (target.matches('button')) {
// TODO : ๊ณ์ฐ๊ธฐ๊ฐ ์๋ํ ์ ์๋๋ก ์๋ ์ฝ๋๋ฅผ ์์ ํ์ธ์.
// ์์ฑ๋์ด ์๋ ์กฐ๊ฑด๋ฌธ๊ณผ console.log๋ฅผ ํ์ฉํ์๋ฉด ์ฝ๊ฒ ๋ฌธ์ ๋ฅผ ํ ์ ์์ต๋๋ค.
// ํด๋ฆญ๋ HTML ์๋ฆฌ๋จผํธ๊ฐ button์ด๋ฉด
if (action === 'number') {
// ๊ทธ๋ฆฌ๊ณ ๋ฒํผ์ ํด๋ ์ค๊ฐ number์ด๋ฉด
// ์๋ ์ฝ๋๊ฐ ์๋๋ฉ๋๋ค.
console.log('์ซ์ ' + buttonContent + ' ๋ฒํผ');
if (firstOperend.textContent === '0'){
firstOperend.textContent = buttonContent;
} else {
secondOperend.textContent = buttonContent
}
}
if (action === 'operator') {
console.log('์ฐ์ฐ์ ' + buttonContent + ' ๋ฒํผ');
}
if (action === 'decimal') {
console.log('์์์ ' + buttonContent + ' ๋ฒํผ');
}
if (action === 'clear') {
console.log('์ด๊ธฐํ ๋ฒํผ');
firstOperend.textContent = '0'
secondOperend.textContent = '0'
calculatedResult.textContent = '0'
operator.textContent ='+'
}
if (action === 'calculate') {
console.log('๊ณ์ฐ ๋ฒํผ');
calculatedResult.textContent = calculate (parseInt(firstOperend.textContent), operator.textContent, parseInt(secondOperend.textContent))
}
}
});
// ! Advanced Challenge test์ Nightmare test๋ฅผ ์ํด์๋ ์๋ ์ฃผ์์ ํด์ ํ์ธ์.
const display = document.querySelector('.calculator__display--for-advanced'); // calculator__display ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
let firstNum, operatorForAdvanced, previousKey, previousNum;
buttons.addEventListener('click', function (event) {
// ๋ฒํผ์ ๋๋ ์ ๋ ์๋ํ๋ ํจ์์
๋๋ค.
const target = event.target; // ํด๋ฆญ๋ HTML ์๋ฆฌ๋จผํธ์ ์ ๋ณด๊ฐ ์ ์ฅ๋์ด ์์ต๋๋ค.
const action = target.classList[0]; // ํด๋ฆญ๋ HTML ์๋ฆฌ๋จผํธ์ ํด๋ ์ค ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
const buttonContent = target.textContent; // ํด๋ฆญ๋ HTML ์๋ฆฌ๋จผํธ์ ํ
์คํธ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
// ! ์ ์ฝ๋๋ ์์ ํ์ง ๋ง์ธ์.
// ! ์ฌ๊ธฐ์๋ถํฐ Advanced Challenge & Nightmare ๊ณผ์ ๋ฃฐ ํ์ด์ฃผ์ธ์.
if (target.matches('button')) {
if (action === 'number') {
if (display.textContent === '0' && previousKey !== 'operator'){
display.textContent = buttonContent
firstNum = display.textContent
} else if (display.textContent !== '0' &&
previousKey !== 'operator' &&
previousNum === undefined){
display.textContent += buttonContent
firstNum = display.textContent
} else if (previousKey === 'operator' && display.textContent !== '0'){
display.textContent = 0;
display.textContent = buttonContent
previousNum = display.textContent
} else {
display.textContent += buttonContent
previousNum = display.textContent
}
previousKey = 'number'
}
if (action === 'operator') {
console.log('์ฐ์ฐ์ ' + buttonContent + ' ๋ฒํผ');
if(firstNum && operatorForAdvanced && previousKey !== 'operator' &&
previousKey !== 'calculate') {
display.textContent = calculate(firstNum, operatorForAdvanced, previousNum)
}
// ๋ณ์ operator์ ๋ฒํผ์ ํ
์คํธ(์ฐ์ฐ์ ๊ธฐํธ, ์ฌ๊ธฐ์๋ '*')๋ฅผ ํ ๋นํฉ๋๋ค.
operatorForAdvanced = buttonContent;
firstNum = parseFloat(display.textContent);
previousKey = 'operator';
}
if (action === 'decimal') {
if (!display.textContent.includes('.') && previousKey !== 'operator'){
display.textContent = display.textContent + '.';
} else if(previousKey === 'operator') {
display.textContent = '0.'
previousNum = '0.'
}
previousKey = 'decimal'
}
if (action === 'clear') {
firstNum = undefined // 1. ๊ณ์ฐ๊ธฐ์ ์
๋ ฅ๋์๋ ์ฒซ ๋ฒ์งธ ๊ฐ
operatorForAdvanced = undefined // 2. ์ฐ์ฐ์
previousNum = undefined // 3. ๊ณ์ฐ๊ธฐ์ ์
๋ ฅ๋์๋ ๋ ๋ฒ์งธ ๊ฐ
display.textContent = 0; // 4. ํ๋ฉด
console.log('์ด๊ธฐํ ๋ฒํผ');
previousKey = 'clear';
}
if (action === 'calculate') {
if (operatorForAdvanced === undefined && previousKey === 'number'){
display.textContent = firstNum
}
else if (previousNum === undefined){
display.textContent = calculate(display.textContent, operatorForAdvanced, display.textContent);
}
else if (previousKey === 'calculate' && operatorForAdvanced){
display.textContent = calculate(display.textContent, operatorForAdvanced, previousNum);
}
else {
display.textContent = calculate(firstNum, operatorForAdvanced, previousNum);
previousKey = 'calculate';
}
console.log(firstNum, operatorForAdvanced, previousNum);
}
}
});
์ผ๋จ ์ด๋ฐ์ advanced ์งํํ๋๋ฐ์๋ ํ์ด๋ถ์ด ๋๋ฌด ์ ๋์์ฃผ์ ์ ๊ฐ์ ํ ์ฑ๊ณตํ๋ค.
๊ทผ๋ฐ ์ค๋ช ๋ ๋ ์ํด์ฃผ์ ์ ์ง์ง ๋์์ด ๋ง์ด ๋จ.. ใ ใ
๊ทธ๋ฌ๋๋ฐ ์ด์ nightmare๋ ๊ฐ์ ํด๋ณด๊ธฐ๋ก ํ๊ณ ํผ์์ ํ๋๋ฐ
์์ซ์ , ์ํฐํค, ์ฐ์ฐ์๋ฅผ ๋๋ ๋๋ฐ ๋ค์์ด ์ ๋์... ์ ใ _ใ
์ซ์ ๋๋ฅด๊ณ ์ํฐใฑํค๋ฅผ ๋๋ฅด๋ฉด ์๊พธ 0์ด ๋์..
๋ณ์๊ฐ ์๊พธ ๋ฐ๋๋๋ฐ ์ด๊ฑฐ ๋ค ๋ณ์ ํ ๋นํ ์ ์๋?
์ง์ง ๋๊ฐ ๋ น์ ๊ฑฐ ๊ฐ์ ๊ธฐ๋ถ ๋๋ผ๋ฉด์... ์ผ๋จ์ ํ๊ธฐ๋ง ์ ์ด๋ณธ๋ค..
์ด ๋ถ๋ถ์ ๊ณ์ ํต๊ณผํ์ง ๋ชปํ๋ค.
์ฐ์ฐ์ ๊ฐ์ด ์๋๋ฐ ๋ฐ๋ก ์ด์ ์ด ์ซ์์์ ๋. ๋ผ๋ ๊ฐ๋จํ ์กฐ๊ฑด๋ฌธ์ด์๋๋ฐ
์ฌ๊ธฐ์ ๊ทธ๋ฅ ๊ณ์ ํค๋งค๊ฐ์ง๊ณ .. ใ ใ ํํธ๋ ๋ค๋ฅธ ์ฌ๋์ด ํ ๊ฑธ ๋ด๋, ๋ด๊ฐ ์ฌํ ๋ณ์ ์ง์ ํด์จ๊ฑฐ๋ฉฐ
๊ทธ๋ฐ๊ฒ ๋ค ๋ฌ๋ผ๊ฐ์ง๊ณ ๋ด ๊ฑฐ์ ๋์ ํด๋ณด์ง ๋ชปํ๊ณ ๊ฒฐ๊ตญ ๋ด๊ฐ ํ์ด๋ด์ผ ํ์....
ํ๊ณ ๋ ์ฃผ๋ง์ ์ ๋๋ก ํด์ผ์ง
์ด์จ๋ ๋ชจ๋ ํต๊ณผ~!! ๋ฟ๋ฏํ๋ค ๋ฟ๋ฏํด ใ _ใ
'Lecture > ์ฝ๋์คํ ์ด์ธ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SEB FE] section 1 unit 10 (2) ์ค์ฝํ (0) | 2022.09.06 |
---|---|
[SEB FE] section 1 unit 10 (1) ์์์๋ฃํ๊ณผ ์ฐธ์กฐ์๋ฃํ + ์ข ํฉํด์ฆ (1) | 2022.09.06 |
[SEB FE] ๊ณ์ฐ๊ธฐ ๋ชฉ์ ๋ง๋ค๊ธฐ (0) | 2022.08.30 |
[SEB FE] Section 1 unit3 (3) - ๋ฐ๋ณต๋ฌธ ๋ฌธ์ ํ๊ธฐ2 [11~16] (0) | 2022.08.28 |
[SEB FE] Section 1 unit3 (3) - ๋ฐ๋ณต๋ฌธ ๋ฌธ์ ํ๊ธฐ1 [1~10] (0) | 2022.08.26 |