Conversation
hoeun0723
left a comment
There was a problem hiding this comment.
궁금한 점 몇가지와, 더 좋은 코드가 무엇일까!? 에 대하여 고민한 부분을 남깁니다:)
좋은 코드 감사해요!!
There was a problem hiding this comment.
오옷!! 혹시 이 파일은 미리 js로 작성해보다가 남겨진 파일인가요 ~??
현재 html에서 js 파일을 로드 하고 있어서 ts로 짠 코드가 작동이 안 되는 거 같아요 ㅠ.ㅠ
There was a problem hiding this comment.
앗 혹시 컴파일한건가요?? 이거 vite라 그냥 했어도 됐는데!
| switch (cal.value) { | ||
| case 'sum': | ||
| result = preNum+postNum; | ||
| break; | ||
| case 'sub': | ||
| result = preNum-postNum; | ||
| break; | ||
| case 'mul': | ||
| result = preNum*postNum; | ||
| break; | ||
| case 'divide': | ||
| result = preNum/postNum; | ||
| break; | ||
| default: | ||
| result=""; | ||
| break; | ||
| } |
There was a problem hiding this comment.
코드 리뷰하다가 생각해본건데,
switch문 말고, 객체 매핑 방식은 어떤가요 ~??
const operations = {
sum: (a, b) => a + b,
sub: (a, b) => a - b,
mul: (a, b) => a * b,
divide: (a, b) => a / b,
};
if (cal.value in operations) {
result = operations[cal.value](preNum, postNum);
} else {
result = "올바른 연산을 선택하세요.";
}
이런식으로요!
코드가 더 간결해지고, 확장 가능성도 높일 수 있을 것이라 생각했어요 !
There was a problem hiding this comment.
옴마나,,,, 진ㅉㅏ 갓갓!! 대신에 타스니까 또 여기에 타입 넣는 거를 잊지 않아야 해용!
`const operations = {
sum: (a: number, b: number): number => a + b,
sub: (a: number, b: number): number => a - b,
mul: (a: number, b: number): number => a * b,
divide: (a: number, b: number): number => a / b,
};
let result: number | string;
if (cal.value in operations) {
result = operations[cal.value](preNum, postNum);
} else {
result = "올바른 연산을 선택하세요.";
}
`
There was a problem hiding this comment.
앗 혹시 컴파일한건가요?? 이거 vite라 그냥 했어도 됐는데!
|
|
||
| const $ = (selector:string) => document.querySelector(selector) as HTMLElement; | ||
|
|
||
| const num1 = $('#num1') as HTMLInputElement;; |
| const num2 = $('#num2') as HTMLInputElement;; | ||
| const cal=$('select[name="calculate"]') as HTMLSelectElement; | ||
| const resultTag=$('h3') as HTMLElement; | ||
| const btn = $('button') as HTMLButtonElement;; |
| switch (cal.value) { | ||
| case 'sum': | ||
| result = preNum+postNum; | ||
| break; | ||
| case 'sub': | ||
| result = preNum-postNum; | ||
| break; | ||
| case 'mul': | ||
| result = preNum*postNum; | ||
| break; | ||
| case 'divide': | ||
| result = preNum/postNum; | ||
| break; | ||
| default: | ||
| result=""; | ||
| break; | ||
| } |
There was a problem hiding this comment.
옴마나,,,, 진ㅉㅏ 갓갓!! 대신에 타스니까 또 여기에 타입 넣는 거를 잊지 않아야 해용!
`const operations = {
sum: (a: number, b: number): number => a + b,
sub: (a: number, b: number): number => a - b,
mul: (a: number, b: number): number => a * b,
divide: (a: number, b: number): number => a / b,
};
let result: number | string;
if (cal.value in operations) {
result = operations[cal.value](preNum, postNum);
} else {
result = "올바른 연산을 선택하세요.";
}
`
| result = preNum*postNum; | ||
| break; | ||
| case 'divide': | ||
| result = preNum/postNum; |
There was a problem hiding this comment.
postNum이 0인 예외으 경우도 빼주면 좋을 것 같아요!
🌮 무엇을: switch case 문으로 각 계산을 나누어 계산기를 구현했습니다!
🌮 어떻게:
🌮 얼마나:
2h🌮 구현 사항:
2023-11-12.00.56.15.mov