-
Couldn't load subscription status.
- Fork 15
[2주차] 추상 데이터 타입, 컬렉션 (백, 스택, 큐, 연결리스트) #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- 각 자료구조의 API 작성
- dequeue() 실행 후 queue 에 아이템이 남아 있지 않다면 #last 는 undefined 를 가리켜야 한다.
| class Node { | ||
| #item; | ||
|
|
||
| #next; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node 객체를 선언할 때 item과 next를 private으로 선언하셨네요. 하지만 코드를 사용하는 곳을 보면 이렇게 사용하고 있습니다.
this.#first.item = item;그래서 원래 의도와는 다르게 퍼블릭 item속성에 접근하고 있는 것 같습니다. 자바스크립트가 타입이 없다보니 이런 실수가 일어난 것 같네요.
그리고 여기서 Node의 속성들은 private일 필요는 없습니다. 왜냐하면 단순한 자료 구조를 나타내는 객체라서 캡슐화가 필요 없기 때문이에요. Stack은 외부에서 first속성을 건드리면 안되기 떄문에 접근하지 못하도록 private으로 선언했지만, Node는 그렇지 않습니다.
problem-3-2/problem-3-2.test.js
Outdated
| const persons = createNumberArray(numberOfPersons); | ||
|
|
||
| const queue = new Queue(); | ||
| persons.forEach((person) => queue.enqueue(person)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enqueue의 반환값이 없기도 하고, forEach는 반환값이 없기도하고, forEach 콜백의 반환값은 있어도 버려지기 때문에 화살표 함수를 쓸 때 코드가 더 짧아진다고 하더라도 분명하게 써주는게 좋습니다. 다른 사람이 봤을 떄는 다른 의도가 있는데 잘못쓴건가? 라고 생각도 들 수 있기 떄문에요.
A function to execute for each element in the array. Its return value is discarded. The function is called with the following arguments:
persons.forEach((person) => {
queue.enqueue(person);
});See also
problem-3-2/problem-3-2.test.js
Outdated
| const solution = (N, M) => { | ||
| const numberOfPersons = N; | ||
| const deathLocation = M; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
변수명을 더 이해하기 쉬운 이름으로 �변경하셨네요. 파라미터를 다시 변수로 할당할 떄가 있는데 이 떄는 주로 파라미터를 다른 값으로 변경하려는 경우가 많아요. 대부분의 언어들은 예를들어 자바나 코틀린 같은 언어들은 파라미터를 수정할 수 없어요. 그러다 보니 변수를 재할당해서 처리할 떄가 있거든요.
fun solution(N: Number, M: Number): Unit {
N = 3 // Val cannot be reassigned
var value = N
value = 2
}물론 const를 써서 그런 의도가 아님을 밝히셨지만, 혹여나 그런 의도로 사용했나 생각이 들 수 있을 것 같아요. 그래서 여기서는 파라미터의 이름을 직접적으로 바꾸면 더 좋았을 것 같습니다
2주차도 화이팅입니다! :)