계산기 목업에 기능 구현하기
bare test
1의 자리수 사칙연산 기능 구현사칙연산 함수 선언
function calculate(n1, operator, n2) {
let result = 0; //매계변수들이 string타입이기 때문에 Number()를 이용해 타입을 변경
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); //return에서 다시 string타입으로 변경
}
계산기 버튼 누를 시 결과창 변경
if (action === 'number') {
console.log('숫자 ' + buttonContent + ' 버튼');
if (firstOperend.textContent === '0') {
firstOperend.textContent = buttonContent;
} else if (firstOperend.textContent !== '0') {
secondOperend.textContent = buttonContent;
}
}
if (action === 'operator') {
console.log('연산자 ' + buttonContent + ' 버튼');
operator.textContent = buttonContent;
}
if (action === 'decimal') {
console.log('소수점 버튼');
}
if (action === 'clear') {
console.log('초기화 버튼');
firstOperend.textContent = '0';
secondOperend.textContent = '0';
operator.textContent = '+';
calculatedResult.textContent = '0';
}
if (action === 'calculate') {
console.log('계산 버튼');
calculatedResult.textContent = calculate(firstOperend.textContent, operator.textContent, secondOperend.textContent);
}
}
}
if (target.matches('button')) {
if (action === 'number') {
if(display.textContent === '0' || previousKey === 'operator'){
display.textContent = buttonContent;
} else {
display.textContent = display.textContent + buttonContent;
}
} previousKey = 'number';
if (action === 'operator') {
operatorForAdvanced = buttonContent;
previousKey = 'operator'
firstNum = display.textContent;
}
if (action === 'decimal') {} //nightmare test
if (action === 'clear') {
firstNum = undefined;
previousKey = undefined;
previousNum = undefined;
operatorForAdvanced =undefined;
display.textContent = '0'; //초기화 버튼이기 때문에 display.textContent 제외하곤 빈 값이 할당 되어야함
}
if (action === 'calculate') {
display.textContent = calculate(firstNum, operatorForAdvanced, display.textContent)
}
}
오늘의 감정
java script
코드를 처음 마주하는 날이었다.
내가 작성하지 않은 코드들이 많이 써있었다. 처음부터 모든걸 쓰기엔 아직 실력이 부족하기 때문인 것 같다.
목업부터 쓰던 변수들과 class이름들로 작성되어 있었다.
덕분에 코드들을 쭉 훑어 봤을 때 무엇을 의미하는지 대충은 짐작할 수 있었다.
이번 페어도 나와 비슷한 실력을 갖고 있는 사람이었다. 운이 좋게 페어가 풀 수 없던 문제는 내가 해결했고 내가 어렵게 생각한 문제는 페어가 해결했다.
그래서 서로 '아 이래서 페어로 하는군요!'라고 외쳤다.
목표는 advanced test 와 bare test였고 딱 맞춰서 과제를 끝냈다. night mare test는 주말을 이용해서 해봐야 할 것 같다.
오늘 줌 세션도 나에게 큰 도움이 되었다. 내가 복잡하게 생각했던 조건문들 몇몇은 꽤나 단순하게 풀 수 있었다.
동기들 중 몇몇은 night mare test까지 다 성공해서 제출했다.
정말 실력자 인 것 같았다.
지난 동기부여 세션에서 남과 나를 비교하지말고 어제의 나와 나를 비교하라고 했다.
그 말이 무슨 뜻인지 점차 이해 하는 중이다.
조건문과 반복문을 처음 할 때 나는 구구단 만들기를 하고 있었는데
오늘 나는 계산기 목업에 기능 구현을 했다.
물론 모든 코드를 혼자 쓰진 않았지만 그래도 꽤나 발전했다.
나는 나보다 잘 하는 사람보다 발전할 것 이 더 많다!