노션으로 보기
Advanced Challenge test
const calculator = document.querySelector('.calculator');
const buttons = calculator.querySelector('.calculator__buttons');
const firstOperend = document.querySelector('.calculator__operend--left');
const operator = document.querySelector('.calculator__operator');
const secondOperend = document.querySelector('.calculator__operend--right');
const calculatedResult = document.querySelector('.calculator__result');
function calculate(n1, operator, n2) {
let result = 0;
if(operator === '+'){
result = Number(n1) + Number(n2);
} else if(operator === '-'){
result = Number(n1) - Number(n2);
} else if(operator === '*'){
result = Number(n1) * Number(n2);
} else if(operator === '/'){
result = Number(n1) / Number(n2);
}
return String(result);
}
buttons.addEventListener('click', function (event) {
const target = event.target;
const action = target.classList[0];
const buttonContent = target.textContent;
if (target.matches('button')) {
console.log(target);
if (action === 'number') {
if(firstOperend.innerText !== '0'){
secondOperend.innerText = buttonContent;
} else {
firstOperend.innerText = buttonContent;
}
}
}
if (action === 'operator') {
operator.innerText = buttonContent;
}
if (action === 'decimal') {
console.log('소수점' + buttonContent + ' 버튼')
}
if (action === 'clear') {
console.log('초기화 버튼');
firstOperend.innerText = '0';
secondOperend.innerText = '0';
operator.innerText = '+';
calculatedResult.innerText = '0';
}
if (action === 'calculate') {
calculatedResult.innerText = calculate(firstOperend.innerText, operator.innerText, secondOperend.innerText);
console.log('계산 버튼');
}
});
const display = document.querySelector('.calculator__display--for-advanced');
let firstNum, operatorForAdvanced, previousKey, previousNum,decimalNum;
firstNum = '';
operatorForAdvanced = '';
previousKey = '';
previousNum = '';
buttons.addEventListener('click', function (event) {
const target = event.target;
const action = target.classList[0];
const buttonContent = target.textContent;
if (target.matches('button')) {
if (action === 'number') {
if(display.textContent === '0' && operatorForAdvanced ===''){
display.textContent = buttonContent;
firstNum = display.textContent;
} else if(display.textContent !== '0' && operatorForAdvanced ==='') {
display.textContent = display.textContent + buttonContent;
firstNum = display.textContent;
} else if(display.textContent !== '0' && operatorForAdvanced !=='') {
display.textContent = buttonContent;
previousNum = display.textContent;
}
}
if (action === 'operator') {
console.log('연산자 ' + buttonContent + ' 버튼');
operatorForAdvanced = buttonContent;
previouskey = operatorForAdvanced;
}
if (action === 'clear') {
display.textContent = '0';
firstNum = '';
operatorForAdvanced = '';
previousKey = 'clear';
previousNum = '';
}
if (action === 'calculate') {
display.textContent =calculate(firstNum, operatorForAdvanced, previousNum);
}
}
});
Nightmare test
문제
- 3, * , 3, enter, enter, enter ⇒ 243
- 3, enter, enter, enter, * , 3 ⇒ 9
- 3, enter, enter, enter, -, 3, enter ⇒ 0
- 3, enter, enter, enter, +, 3, enter ⇒ 6
- 3, enter, enter, enter, /, 3, enter ⇒ 1
- 3, * , enter ⇒ 9
- 3, -, enter ⇒ 0
if (action === 'number') {
if(display.textContent === '0' && operatorForAdvanced ===''){
display.textContent = buttonContent;
firstNum = display.textContent;
} else if(display.textContent !== '0' && operatorForAdvanced ==='') {
display.textContent = display.textContent + buttonContent;
firstNum = display.textContent;
} else if(display.textContent !== '0' && operatorForAdvanced !=='') {
display.textContent = buttonContent;
previousNum = display.textContent;
}
} else if(display.textContent !== '0' && operatorForAdvanced !=='' && previousKey === operatorForAdvanced) {
display.textContent = buttonContent;
previousKey = display.textContent;
previousNum = display.textContent;
} else if(display.textContent !== '0' && operatorForAdvanced !=='' && previousKey !== operatorForAdvanced) {
display.textContent = display.textContent + buttonContent;
previousNum = display.textContent;
}
} else if(display.textContent !== '0' && operatorForAdvanced !=='') {
if(previousKey === operatorForAdvanced) {
display.textContent = buttonContent;
previousKey = display.textContent;
previousNum = display.textContent;
} else if(previousKey !== operatorForAdvanced) {
display.textContent = display.textContent + buttonContent;
previousNum = display.textContent;
}
}
}