Conversation
hoeun0723
left a comment
There was a problem hiding this comment.
너무너무 훌륭한 코드여서
에러가 발생할 수 있는 부분에 대한 에러처리에 집중한 코드 리뷰를 했네요..
좋은 코드 작성해주셔서 감사합니다 !!! 🔥😆 많이 배워가요 !!
|
|
||
| switch (selectorOptions) { | ||
| case "sum": | ||
| result = num1 + num2; | ||
| break; | ||
| case "sub": |
There was a problem hiding this comment.
시작부분에 입력이 유효한지 확인하고 오류 처리를 추가해주는 것은 어떤가요 ??
if (isNaN(num1) || isNaN(num2)) {
result = "숫자를 입력하세요";
} else {
이런식으로요 !!
하지만 필수적인 요소는 아니라고 생각합니다 :)
| const $ = (selector: string) => | ||
| document.getElementById(selector) as HTMLElement; |
There was a problem hiding this comment.
DOM 요소를 가져오는데, ID가 존재하지 않아 nul을 반환할 수 있으니, null 체크와 에러처리를 추가해주는 것은 어떤가요 ??
const $ = (selector: string) => {
const element = document.getElementById(selector);
if (!element) {
throw new Error(`Element with ID '${selector}' not found.`);
}
return element as HTMLElement;
};
There was a problem hiding this comment.
저는 null 관련 지정을 안 했더니 에러가 무진장 나서 냅다 내 코드에서 null은 발생할 수 없어. 라고 !통해서 지정해두고 넘어갔는데 .... 혠니 코드에서는 따로 null관련 코드가 없는데도 에러 발생 없이 잘 작동했나보군요 ! 신기하네용 .. 뭔가 접근 방식에 따라 null에러가 발생하는 건가 ..?
There was a problem hiding this comment.
저만 그랬던게 아니군요...허허 저는 이번주에 배운 | null 을 붙여서 해결했는데, 혜인언니 코드에는 없어서 어떻게 해결했는지 궁금합니당
| switch (selectorOptions) { | ||
| case "sum": | ||
| result = num1 + num2; | ||
| break; | ||
| case "sub": | ||
| result = num1 - num2; | ||
| break; | ||
| case "mul": | ||
| result = num1 * num2; | ||
| break; | ||
| case "divide": | ||
| if (num2 !== 0) { | ||
| result = num1 / num2; | ||
| } else { | ||
| result = "계산 불가"; | ||
| } | ||
| 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) => (b !== 0 ? a / b : "계산 불가"),
};
if (selectorOptions in operations) {
result = operations[selectorOptions](num1, num2);
} else {
result = "";
}
이런식으로요!
코드가 더 간결해지고 확장 가능성이 높아질 것이라고 생각했습니다 !!
There was a problem hiding this comment.
그그 그렇다면 호은이 코드 리뷰를 적용해보려면 객체를 interface로 만들게 되는 것인가요 ?
혹은 interface로 key-value 타입 지정을 해두고,
const operations의 타입에 만들어둔 interface를 대입하면 되는 것인가 ..?
interface Operations {
[key: string]: (a: number, b: number) => number | string;
}
const operations : Operations {
...
}이렇게 ..? 🥹
| function blockString(inputDom: HTMLInputElement): void { | ||
| inputDom.addEventListener("input", (e) => { | ||
| const input = e.target as HTMLInputElement; | ||
| const changeToNum = input.value.replace(/[^0-9.]/g, ""); |
There was a problem hiding this comment.
오 이렇게 하면 잘못 해서 문자가 입력돼도 그냥 생략되는건가용?
ex) 12.a23 => 12.23 이런식으로??
se0jinYoon
left a comment
There was a problem hiding this comment.
꼼꼼한 예외처리 감탄하면서 코드리뷰하고 감니당 !!
많이 배워가요오 💗
| const $ = (selector: string) => | ||
| document.getElementById(selector) as HTMLElement; |
There was a problem hiding this comment.
저는 null 관련 지정을 안 했더니 에러가 무진장 나서 냅다 내 코드에서 null은 발생할 수 없어. 라고 !통해서 지정해두고 넘어갔는데 .... 혠니 코드에서는 따로 null관련 코드가 없는데도 에러 발생 없이 잘 작동했나보군요 ! 신기하네용 .. 뭔가 접근 방식에 따라 null에러가 발생하는 건가 ..?
| alert("숫자입력하세요"); | ||
| input.value = ""; |
| switch (selectorOptions) { | ||
| case "sum": | ||
| result = num1 + num2; | ||
| break; | ||
| case "sub": | ||
| result = num1 - num2; | ||
| break; | ||
| case "mul": | ||
| result = num1 * num2; | ||
| break; | ||
| case "divide": | ||
| if (num2 !== 0) { | ||
| result = num1 / num2; | ||
| } else { | ||
| result = "계산 불가"; | ||
| } | ||
| break; | ||
| default: | ||
| result = ""; | ||
| break; | ||
| } |
There was a problem hiding this comment.
그그 그렇다면 호은이 코드 리뷰를 적용해보려면 객체를 interface로 만들게 되는 것인가요 ?
혹은 interface로 key-value 타입 지정을 해두고,
const operations의 타입에 만들어둔 interface를 대입하면 되는 것인가 ..?
interface Operations {
[key: string]: (a: number, b: number) => number | string;
}
const operations : Operations {
...
}이렇게 ..? 🥹
rachel5640
left a comment
There was a problem hiding this comment.
똑같이 switch문을 사용해서 구현했는데, 섬세하게 예외처리해준 부분들 보면서 배워가요!!
스장님 언제나 화이팅💛
| const $ = (selector: string) => | ||
| document.getElementById(selector) as HTMLElement; |
There was a problem hiding this comment.
저만 그랬던게 아니군요...허허 저는 이번주에 배운 | null 을 붙여서 해결했는데, 혜인언니 코드에는 없어서 어떻게 해결했는지 궁금합니당
| case "divide": | ||
| if (num2 !== 0) { | ||
| result = num1 / num2; | ||
| } else { | ||
| result = "계산 불가"; | ||
| } |
SooY2
left a comment
There was a problem hiding this comment.
제가 놓쳤던 부분들을 많이 알 수 있었던... 다음 타스 스터디 때 혜인언니 코드로 설명 한번 해주시면 넘 좋을거같아요🥺 많이 배워 갑니다롱
| function blockString(inputDom: HTMLInputElement): void { | ||
| inputDom.addEventListener("input", (e) => { | ||
| const input = e.target as HTMLInputElement; | ||
| const changeToNum = input.value.replace(/[^0-9.]/g, ""); |
| result = num1 * num2; | ||
| break; | ||
| case "divide": | ||
| if (num2 !== 0) { |
🌮 무엇을:
🌮 어떻게:
const $ = (selector: string) => document.getElementById(selector) as HTMLElement;getElementById를 좀더 쉽게 할 수 있는 함수를 만들어 공통적인부분을 최소화 했습니다.
input부터 어떻게 해부자! 여서 좀 걸렸는데, 그냥 버튼에 모든 걸 다 부여해서 해결했습니다.const resultHere = $("result") as HTMLHeadingElement;as를 이용해서 타입을 부여하였더니event.target.value등과 같은 부분에서 ts에러가 나지 않았습니다!input type =text로 변경ㅎㅏ고, 문자를 입력할 경우 alert가 뜨는 함수도 구현하였습니다. 처음에 typeof 를 썻는데 생각해보니 다 string 처리더군요,,, 그래서 number인 요소들과 입력값을 비교하는 함수를 만들어 처리했습니다.HTMLDOM을 함수의 parameter로 넘겨주는 걸 생각 못해 좀 헤맷음,,,솔직히 구글링 오져따리,,ㅎㅎㅎㅎ내가 혼자 한 게 별로 없는 것 같은 자괴감!!! 😢
🌮 얼마나:
🌮 구현 사항:
_2023_11_04_09_52_01_874.mp4