Conversation
hochan222
left a comment
There was a problem hiding this comment.
우와... 엄청 깔끔하네요.. 무엇보다 기능별로 잘 나뉘어져 있어서 좋았습니다. 저도 다음에 짤때는 부분부터 먼저 나누고 짜야겠습니다. 고생하셨습니다 :)
| .vscode-test | ||
|
|
||
| # javascript source file | ||
| src/**/*.js |
| @@ -0,0 +1,39 @@ | |||
| interface IEquation { | |||
There was a problem hiding this comment.
저도 다음번엔 인터페이스를 활용해봐야겠어요 !
단순 멤버변수로 뒀을 수도 있을 텐데, 인터페이스 상속해온 이유가 있나요?
There was a problem hiding this comment.
사실 여기서는 큰 의미가 없습니다. 인터페이스를 구현하여 속성을 강제한다거나, 확장을 위해 클래스에 인터페이스를 구현한다고 하는데요. 우선은 조금이나마 익숙해지기 위해 인터페이스를 사용해보았습니다.
| } | ||
| } | ||
| clickOperations(target: HTMLButtonElement, equation: Equation) { | ||
| if (target.className !== "operation") { |
| document | ||
| .querySelector("div.modifiers.subgrid")! |
There was a problem hiding this comment.
| document | |
| .querySelector("div.modifiers.subgrid")! | |
| $("div.modifiers.subgrid")! |
https://github.com/transcendence42/javascript-calculator/pull/4/files#r645262877
| return; | ||
| } | ||
| const operation: string = target.innerText; | ||
| const num = Number(this.totalDiv.innerText); |
There was a problem hiding this comment.
| const num = Number(this.totalDiv.innerText); | |
| const num: number = Number(this.totalDiv.innerText); |
저는 적는게 좋다고 생각하는데 혹시 어떻게 생각하시나요?
There was a problem hiding this comment.
그게 맞는 것 같습니다. 타입스크립트 쓰는 김에 타입을 적을 수 있는 부분은 모두 적는게 좋은 것 같아요.
jwon42
left a comment
There was a problem hiding this comment.
코드가 간결하고 가독성이 좋아 전체 코드 중 가장 읽기 편했습니다.
클래스를 사용하면 클래스 내부에서 공통적으로 사용되는 변수들에 대해 정의하고 사용할 수 있기 때문에 마치 전역변수를 사용하는 것 같은 효과가 있군요.
수고하셨습니다 :)
| private plus(equation: Equation): number { | ||
| return equation.firstNum + equation.secondNum; | ||
| } | ||
| private minus(equation: Equation): number { | ||
| return equation.firstNum - equation.secondNum; | ||
| } | ||
| private multiply(equation: Equation): number { | ||
| return equation.firstNum * equation.secondNum; | ||
| } | ||
| private divide(equation: Equation): number { | ||
| return Math.floor(equation.firstNum / equation.secondNum); | ||
| } |
There was a problem hiding this comment.
연산 부분을 함수로 분리해두어서 가독성이 굉장히 좋습니다.
다만 한 줄로 표현될 수 있는 내용이 함수로 분리되어 비용 지출이 있는건 아닐지 고민해보아야 할 듯 합니다.
| interface IEquation { | ||
| firstNum: number; | ||
| secondNum: number; | ||
| operation: string; | ||
| } |
There was a problem hiding this comment.
typedef 하는 느낌으로 interface를 사용하셨다고 이해하면 될까요?
There was a problem hiding this comment.
약간 다릅니다. 큰 의미는 없지만 클래스에서 해당 변수를 사용하도록 강제하기 위해 인터페이스를 사용하였습니다.
| if (target.className !== "digit") { | ||
| return; | ||
| } | ||
| if (isNaN(Number(this.totalDiv.innerText))) { |
There was a problem hiding this comment.
inNaN의 경우 문자 가장 앞에 0x가 오는 경우 16진수로 이해하기 때문에 0을 대상으로 한 연산도 진행하기 위해서 정규표현식을 이용한 별도의 숫자여부 체크 함수를 만들어 사용했었습니다.
yechoi42
left a comment
There was a problem hiding this comment.
제일 좋았던 건 equeation이라는 클래스에서 firstNum, secondNum, operation 정보를 저장해두는 부분이었습니다. 이 자료 형태가 있어서 코드가 전반적으로 깔끔했던 것 같아요. 별도의 파싱 과정도 필요 없었고요. 잘 보고 갑니다 ~
| @@ -0,0 +1,39 @@ | |||
| interface IEquation { | |||
There was a problem hiding this comment.
저도 다음번엔 인터페이스를 활용해봐야겠어요 !
단순 멤버변수로 뒀을 수도 있을 텐데, 인터페이스 상속해온 이유가 있나요?
| setFirstNum(num: number) { | ||
| this.firstNum = num; | ||
| } |
There was a problem hiding this comment.
객체 리터럴 안에서 getter와 setter 메서드는 get과 set으로 나타낼 수 있습니다.
아래처럼 사용하는데요
let user = {
get name() {
return this._name;
},
set name(value) {
this._name = value;
}
};
user.name = "Pete";
alert(user.name); // Pete
이것도 set FirstNum(num: number)로 만들어두면 Equation.firstNum = number 이런 식으로 사용해볼 수 있을 것 같습니다.
| this.totalDiv = document.querySelector("#total") as HTMLDivElement; | ||
| document |
| if (target.className !== "digit") { | ||
| return; |
There was a problem hiding this comment.
querySelector("div.digits.flex")에 더해진 이벤트 리스너인데 target.className이 digit이 아닐 때도 있을까요?
| } | ||
| if (this.totalDiv.innerText.length < 3) { | ||
| this.totalDiv.insertAdjacentText("beforeend", target.innerText); | ||
| this.totalDiv.innerText = String(Number(this.totalDiv.innerText)); |
There was a problem hiding this comment.
이미 innerText는 string일텐데 한번더 형변환 처리해주는 이유가 뭔가요??
There was a problem hiding this comment.
숫자로 변환한 값을 innerText에 넣기 위해 다시 string으로 형변환을 합니다.
|
퇴고가 필요한 것 같습니다... 부끄럽게도 어제 완성한 코드인데 if문이 어떤 케이스 때문에 사용되는지 바로 이해가 되지 않았네요... |

테스트 케이스에 대해 많이 고민하고 프로그램을 설계해야 기능을 추가하거나 수정할 때 어려움이 적은 것 같습니다.