Skip to content

[이동현] up and down 2단계#12

Open
D5ng wants to merge 58 commits intowhatever-mentoring:mainfrom
D5ng:mission-2
Open

[이동현] up and down 2단계#12
D5ng wants to merge 58 commits intowhatever-mentoring:mainfrom
D5ng:mission-2

Conversation

@D5ng
Copy link

@D5ng D5ng commented Feb 16, 2025

✅ 요구사항

  • 게임을 시작하면 최소 값, 최대 값, 진행 가능 횟수를 유저에게 물어본다.
  • 컴퓨터는 유저가 입력한 최소 값, 최대 값 사이의 랜덤 숫자를 생성한다.
  • 사용자는 숫자를 추측하여 입력한다. 입력마다 "업", "다운", "정답!" 중 하나를 출력한다.
  • 사용자의 이전 추측들을 보여준다.
  • 사용자는 입력한 진행 가능 횟수까지 정답을 맞출 수 있으며, 입력한 진행 가능 횟수 이내동안에는 맞출 때까지 반복한다.
  • 게임이 끝나면 사용자가 몇 회만에 맞췄는지 출력한다.
  • 게임을 재시작하거나 종료할 수 있는 옵션을 제공한다.

📝 구현 내용

  • JSDoc을 적극 사용 (TS 대안)
  • 유저가 잘못된 값 입력 시 에러 메세지를 출력하고, 해당 prompt를 다시 실행하도록 구현
  • 클로저를 활용한 상태 관리 구현
    • 불변성으로 업데이트하도록 구현.
    • setState시 함수형 업데이트로 state 의존성 분리

커밋이 많이 더러워졌습니다... 코드를 push 했을 당시엔 최선이였는데, 하루가 지날 때마다 더 좋은 생각(?)이 나서 수정하다보니 이렇게 됐습니다 😂

D5ng added 27 commits January 15, 2025 09:45
setState({ min, max, answer })
} catch (error) {
console.log(error)
throw new Error("최소값, 최대값을 설정하는데 에러가 발생했어요.")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이러면 에러전파되는게 큰 의미가 없어보이네요.
error.message가 있다면 내용을 보여주고 없다면 기본 에러 메세지를 설정하는게 좋은것같네요

import { state } from "../model"
import { readLineAsync, NumberTypeError } from "../utils"

export async function start() {
Copy link
Author

@D5ng D5ng Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여러 로직을 포함하다보니, 맥락을 파악하기 힘드네요. 각 로직을 함수로 분리하는것이 좋아보입니다.

return false
}

function resetGameSettings() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존에 만든 setState로 상태를 업데이트하는게 좋아보입니다.

현 코드에선 state를 변경해도 실제 상태에서는 변경되지 않는 문제가 있어요.

@@ -0,0 +1,6 @@
export class NumberTypeError extends Error {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

custom error를 어떻게 사용하는게 좋을지 고민 해야합니다.

D5ng added 30 commits February 18, 2025 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant