From b07d8161ecdc82eefea854275d580ec72046d2e8 Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 14:20:17 +0900 Subject: [PATCH 1/8] =?UTF-8?q?Docs=20:=20=EB=AC=B8=EC=A0=9C=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=EC=82=AC=ED=95=AD=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..4177cf3 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,21 @@ +## 자동차 이름 입력받기 + +- 자동차 이름을 입력받는다. +- 이름이 하나일 경우를 처리한다. +- 입력은 쉼표로 구분한다. 쉼표와 문자가 아닌 다른 기호가 들어오면 예외 처리한다. +- 6자 이상의 이름은 예외 처리한다. + +## 시도 횟수 입력받기 + +- 시도할 횟수를 입력받는다. + +## 라운드 시작, 자동차 주행 + +- 시도할 횟수만큼 라운드 (랜던 연산을 모든 자동차에게 수행하는 작업) 를 시작한다. +- 0부터 9까지 각각의 자동차별로 랜덤 연산을 수행하고, 무작위 값이 4 이상일 때 carStatus 배열에서 해당 인덱스의 요소값을 올린다. +- 요소값을 올리면서 최대값도 저장하여 최대값을 구할 수 있도록 한다. + +## 라운드 종료 + +- 모든 연산을 실행하고 난 뒤, carStatus 배열에서 인덱스 별 값을 확인해 출력할 수 있게 한다. +- 또한 구한 최대값을 통해 공동 우승자 또는 단독 우승자를 찾아 출력할 수 있도록 한다. From 0954fe4fd345aba262b39fae7852eb8bd5e4a9ed Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 14:20:33 +0900 Subject: [PATCH 2/8] =?UTF-8?q?Feat=20:=20=EC=9E=85=EB=A0=A5=20=EB=8B=A8?= =?UTF-8?q?=EA=B3=84=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index c38b30d..07b61d7 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,31 @@ +import { Console } from "@woowacourse/mission-utils"; + class App { - async play() {} + async play() { + const names = await this.getCarname(); + const times = await this.getTimes(); + } + async getCarname() { + try { + const input = await Console.readLineAsync( + "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n" + ); + const names = Array.from(input.split(",").map((name) => name.trim())); + return names; + } catch (error) { + // reject 되는 경우 + } + } + + async getTimes() { + try { + const input = await Console.readLineAsync("시도할 횟수는 몇 회인가요?\n"); + const times = parseInt(input); + return times; + } catch (error) { + // reject 되는 경우 + } + } } export default App; From 2f9453e813130a6bcb026c50811e3dcfc8e9f9f9 Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 14:48:26 +0900 Subject: [PATCH 3/8] =?UTF-8?q?Docs=20:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=85?= =?UTF-8?q?=EC=84=B8=EC=84=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 4177cf3..1c6ccc3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@ ## 라운드 시작, 자동차 주행 - 시도할 횟수만큼 라운드 (랜던 연산을 모든 자동차에게 수행하는 작업) 를 시작한다. -- 0부터 9까지 각각의 자동차별로 랜덤 연산을 수행하고, 무작위 값이 4 이상일 때 carStatus 배열에서 해당 인덱스의 요소값을 올린다. +- 0부터 9까지 각각의 자동차별로 랜덤 연산을 수행하고, 무작위 값이 4 이상일 때 race 배열에서 해당 인덱스의 요소값을 올린다. - 요소값을 올리면서 최대값도 저장하여 최대값을 구할 수 있도록 한다. ## 라운드 종료 From 06e726ab7738d92338ad425399cf9af80a4dc542 Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 14:58:02 +0900 Subject: [PATCH 4/8] =?UTF-8?q?Feat=20:=20=EA=B2=BD=EC=A3=BC=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/App.js b/src/App.js index 07b61d7..62db612 100644 --- a/src/App.js +++ b/src/App.js @@ -1,9 +1,22 @@ import { Console } from "@woowacourse/mission-utils"; +import { MissionUtils } from "@woowacourse/mission-utils"; class App { async play() { const names = await this.getCarname(); const times = await this.getTimes(); + const race = Array(names.length).fill(0); + let max = 0; + + for (let i = 0; i < times; i++) { + names.forEach((name, index, array) => { + race[index] = + MissionUtils.Random.pickNumberInRange(0, 9) >= 4 + ? race[index] + 1 + : race[index]; + max = race[index] > max ? race[index] : max; + }); + } } async getCarname() { try { From 830b96b476ada9ebf8c9fb8ff65df78fc885c4bd Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 15:07:51 +0900 Subject: [PATCH 5/8] =?UTF-8?q?Feat=20:=20=EA=B2=BD=EC=A3=BC=20=EB=8B=A8?= =?UTF-8?q?=EA=B3=84=20=EC=B6=9C=EB=A0=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/App.js b/src/App.js index 62db612..2515fd6 100644 --- a/src/App.js +++ b/src/App.js @@ -8,6 +8,8 @@ class App { const race = Array(names.length).fill(0); let max = 0; + Console.print("\n실행 결과"); + for (let i = 0; i < times; i++) { names.forEach((name, index, array) => { race[index] = @@ -15,7 +17,9 @@ class App { ? race[index] + 1 : race[index]; max = race[index] > max ? race[index] : max; + Console.print(`${name} : ${"-".repeat(race[index])}`); }); + Console.print(""); } } async getCarname() { From c6c019bc95a6c7613048eaa312fd246d7d9650d6 Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 15:16:49 +0900 Subject: [PATCH 6/8] =?UTF-8?q?Feat=20:=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/App.js b/src/App.js index 2515fd6..6c5cd1c 100644 --- a/src/App.js +++ b/src/App.js @@ -21,6 +21,12 @@ class App { }); Console.print(""); } + + Console.print( + `최종 우승자 : ${names + .filter((name) => race[names.indexOf(name)] >= max) + .join(", ")}` + ); } async getCarname() { try { From da97b880d694e8e8ba75a55722fd519073ca7fe8 Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 16:33:10 +0900 Subject: [PATCH 7/8] =?UTF-8?q?Feat=20:=20=EC=97=90=EB=9F=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84(=EB=AF=B8=EC=99=84=EB=A3=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index 6c5cd1c..75d351c 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,13 @@ import { Console } from "@woowacourse/mission-utils"; import { MissionUtils } from "@woowacourse/mission-utils"; +class InputError extends Error { + constructor(message) { + super(message); + this.name = "[ERROR]"; + } +} + class App { async play() { const names = await this.getCarname(); @@ -34,9 +41,17 @@ class App { "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n" ); const names = Array.from(input.split(",").map((name) => name.trim())); + if (names.length < 2) { + throw new InputError( + "이름을 올바르게 입력해 주세요. (쉼표로 구분, 2개 이상의 이름)\n" + ); + } return names; } catch (error) { - // reject 되는 경우 + if (error instanceof InputError) { + throw error; + } + throw error; } } @@ -44,9 +59,15 @@ class App { try { const input = await Console.readLineAsync("시도할 횟수는 몇 회인가요?\n"); const times = parseInt(input); + if (!isNaN(input)) { + throw new InputError("시도할 횟수는 숫자로 입력하세요."); + } return times; } catch (error) { - // reject 되는 경우 + if (error instanceof InputError) { + throw error; + } + throw error; } } } From 2b662dfa1183c34d1476dce1e971204be80a772f Mon Sep 17 00:00:00 2001 From: cho4u4o Date: Sat, 7 Sep 2024 22:44:39 +0900 Subject: [PATCH 8/8] Fix : Error fixed --- src/App.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/App.js b/src/App.js index 75d351c..ec180fe 100644 --- a/src/App.js +++ b/src/App.js @@ -48,9 +48,6 @@ class App { } return names; } catch (error) { - if (error instanceof InputError) { - throw error; - } throw error; } } @@ -59,14 +56,11 @@ class App { try { const input = await Console.readLineAsync("시도할 횟수는 몇 회인가요?\n"); const times = parseInt(input); - if (!isNaN(input)) { - throw new InputError("시도할 횟수는 숫자로 입력하세요."); + if (isNaN(input)) { + throw new InputError("[ERROR] 숫자가 잘못된 형식입니다."); } return times; } catch (error) { - if (error instanceof InputError) { - throw error; - } throw error; } }