Conversation
hae2ni
left a comment
There was a problem hiding this comment.
굿굿 수고했어! 타스 첨이랑 많이 어려웠을텐데 왕왕 고생많이해떠!
| if (num2 !== 0) { | ||
| return num1 / num2; | ||
| } else { | ||
| return NaN; |
There was a problem hiding this comment.
NaN보단 확실히 '계산이 안된다"를 나타내는 표지 반영이 좋을 것 같아옹
| ) as HTMLButtonElement | null; | ||
|
|
||
| function calculate(num1: number, num2: number, operator: Operator): number { | ||
| switch (operator) { |
| } | ||
| } | ||
|
|
||
| if (input1 && input2 && sign && result && resultBtn) { |
There was a problem hiding this comment.
다 있을 때 처리해준 거 넘 좋아요! resultBtn같은 거는 이미 있는 거니까 없을 수도 있고 있을 수도 있는 값들만 체크해주는 건 어떨까 싶은데!!
|
|
||
| if (input1 && input2 && sign && result && resultBtn) { | ||
| function displayResult() { | ||
| const num1 = parseFloat(input1?.value || "0"); |
se0jinYoon
left a comment
There was a problem hiding this comment.
나도 타스가 처음인지라 .... 민서 코드리뷰 하면서 저 헷갈렸던 부분들 정리할 수 있었어용 🧚🏻♀️
수고 많았어요오 💗💗
| @@ -0,0 +1,40 @@ | |||
| type Operator = "sum" | "sub" | "mul" | "divide"; | |||
There was a problem hiding this comment.
이번 주차 아티클 작성하고 와서 보니깐 눈에 보이는 코드 💡
유니언을 배우기 전에 사용했다니 멋져 ....
| const num1 = parseFloat(input1?.value || "0"); | ||
| const num2 = parseFloat(input2?.value || "0"); |
There was a problem hiding this comment.
저도 이번에 하면서 알게 된 건데 ! 민서 코드에서도 보여서 생각난 김에 코드리뷰하면서 정리 한 번 하고 갈게요 히히
?. : optional chaining
input1 이 null 또는 undefined 이어도 오류가 발생하지 않고 undefined를 반환한다
|| : nullish coalescing
input1의 value가 falsy인 경우 0을 기본값으로 제공한다
| } else { | ||
| return NaN; |
There was a problem hiding this comment.
여기 NaN보다는 에러 문구같은 걸 리턴하면 어떨까요?
예를들면 0으로 나눌 수 없습니다! 같은 것!
| const input1 = document.getElementById("input1") as HTMLInputElement | null; | ||
| const input2 = document.getElementById("input2") as HTMLInputElement | null; | ||
| const sign = document.getElementById("sign") as HTMLSelectElement; | ||
| const result = document.getElementById("result") as HTMLHeadingElement; | ||
| const resultBtn = document.getElementById( | ||
| "resultBtn" | ||
| ) as HTMLButtonElement | null; |
There was a problem hiding this comment.
난 null 에러가 계속 나서 냅다 null일 수 없다! 라고 !연산자 통해서 제한하고 시작했는데 이렇게 핸들링할 수 있는 거였구나 ,,,,
🌮 무엇을:
🌮 어떻게:
input1?.valueinput2?.value을 사용하여, 만약 input1 또는 input2가 null이라면 0을 기본값으로 사용하게 되며, 코드 실행 중 null 오류가 발생하지 않게🌮 얼마나:
4시간: 타입스크립트가 처음이었는데,정말 오류가 많이나더라구요...
변수가 null일 가능성을 모두 빼느라고 시간을 조금 썼어요🥹
: 커밋을 완전히 깜빡하고 안남겼어요...다음에는 더 잘게 쪼개보겠습니다..
🌮 구현 사항:
2023-11-08.12.49.21.mov