-
Notifications
You must be signed in to change notification settings - Fork 173
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
[클린코드 6기 정민지] 로또 미션 STEP 2 #284
Open
jungminji0215
wants to merge
4
commits into
next-step:jungminji0215
Choose a base branch
from
jungminji0215:step2
base: jungminji0215
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,26 +10,36 @@ import { WinningLotto } from "./js/domain/WinningLotto.js"; | |
import { StatisticsLotto } from "./js/domain/StatisticsLotto.js"; | ||
|
||
async function play() { | ||
const purchase = await readLineAsync("> 구입금액을 입력해 주세요. "); | ||
let lottoMachine; | ||
let count; | ||
let purchase; | ||
let lottos = []; | ||
|
||
const lottoMachine = new LottoMachine(); | ||
const count = lottoMachine.calculateLottoCount(purchase); | ||
printCount(count); | ||
while (true) { | ||
try { | ||
purchase = await readLineAsync("> 구입금액을 입력해 주세요. "); | ||
lottoMachine = new LottoMachine(); | ||
count = lottoMachine.calculateLottoCount(purchase); | ||
printCount(count); | ||
|
||
const lottos = Array.from({ length: count }, () => { | ||
const lotto = lottoMachine.generateLottoNumber(); | ||
const lottoNumbers = lotto.getLottoNumbers(); | ||
printLottoNumber(lottoNumbers); | ||
return lottoNumbers; | ||
}); | ||
lottos = Array.from({ length: count }, () => { | ||
const lotto = lottoMachine.generateLottoNumber(); | ||
const lottoNumbers = lotto.getLottoNumbers(); | ||
printLottoNumber(lottoNumbers); | ||
return lottoNumbers; | ||
}); | ||
|
||
break; | ||
} catch (error) { | ||
console.error("오류가 발생했습니다: ", error); | ||
} | ||
Comment on lines
+19
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 질문, 입력값 변환, 에러 출력등의 코드를 함수로 분리하면 try/catch 구문과 같이 가독성을 해치는 코드들이 추상화되어 가독성이 개선될 것 같습니다. 다음 예시 코드를 보고 비슷하게 구현해봐도 좋을 것 같아요! const prompt = ({
query, // 질문
validate, // 입력값 검증
transform // 입력값 변환
}) => {
try {
// 질문
// 입력값 검증
// 입력값 변환 및 반환
} catch(e) {
// 에러 출력
}
}
// 질문 프롬프트 로직을 추상화한 예시
const lottos = prompt({
query: "> 구입금액을 입력해 주세요.",
validate: LottoMachine.isValidPurchaseAmount,
transform: (answer) => {
lottoMachine = new LottoMachine();
count = lottoMachine.calculateLottoCount(answer);
printCount(count);
lottos = Array.from({ length: count }, () => {
const lotto = lottoMachine.generateLottoNumber();
const lottoNumbers = lotto.getLottoNumbers();
printLottoNumber(lottoNumbers);
return lottoNumbers;
});
}
}) |
||
} | ||
const winningLottoNumbers = await readLineAsync( | ||
"\n> 당첨 번호를 입력해 주세요. " | ||
); | ||
|
||
const bonusNumber = await readLineAsync("\n> 보너스 번호를 입력해 주세요. "); | ||
|
||
// when | ||
const winningLotto = new WinningLotto(winningLottoNumbers, bonusNumber); | ||
|
||
const statisticsLotto = new StatisticsLotto(); | ||
|
@@ -45,6 +55,21 @@ async function play() { | |
// 총 수익률 출력 | ||
const rateOfReturn = statisticsLotto.calculateRateOfReturn(purchase); | ||
printRateOfReturn(rateOfReturn); | ||
|
||
await reply(statisticsLotto); | ||
} | ||
|
||
async function reply(statisticsLotto) { | ||
const replyAnswer = await readLineAsync("\n> 다시 시작하시겠습니까? (y/n)\n"); | ||
|
||
if (isReply(replyAnswer)) { | ||
statisticsLotto.resetCounts(); | ||
await play(); | ||
} | ||
} | ||
|
||
function isReply(replyAnswer) { | ||
return replyAnswer === "y"; | ||
} | ||
|
||
play(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
5000같은 것도 prizes 객체에서 참조해오면 좋을 것 같네요