From a90f83317395c7fef58bc1b24d5ca572d22c2a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 18:46:47 +0900 Subject: [PATCH 01/12] =?UTF-8?q?docs(readme):=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=ED=95=A0=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index 15bb106b5..b41b73c78 100644 --- a/README.md +++ b/README.md @@ -1 +1,43 @@ # javascript-lotto-precourse + +### 기능 구현 목록 + +1. 입력 + + 1-1. 로또 구입 금액 입력 + + - 1,000원 단위로 입력 받으며 1,000원으로 나누어 떨어지지 않는 경우 예외 처리 + + 1-2. 당첨 번호 입력 + + - 6개의 당첨 번호, 쉼표(,)를 기준으로 구분 + + 1-3. 보너스 번호 입력 + +2. 기능 + + 2-1. 발행해야 할 로또 개수 구하기 + + 2-2. 로또 숫자 뽑기 + + - 중복되지 않는 6개의 숫자 + + - 6개의 숫자와 중복되지 않는 보너스 번호 1개 뽑기 + + 2-3. 로또 당첨 여부 확인 + + - 4개 일치하는 경우, 보너스 볼과 일치하는지 확인 + +3. 출력 + + 3-1. 발행한 로또 수량 및 번호 출력 + + - 오름차순으로 정렬하여 출력 + + 3-2. 당첨 내역 출력 + + 3-3. 수익률 출력 + + - 소수점 둘째 자리에서 반올림 + + 3-4. 로또 게임 종료 From aae51d12daca92ec0a295fcc9901af27c3ba1a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 20:49:15 +0900 Subject: [PATCH 02/12] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EC=9E=85=20=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 20 +++++++++++++++++++- src/InputComponent.js | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/InputComponent.js diff --git a/src/App.js b/src/App.js index 091aa0a5d..f068b7825 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,23 @@ +import { Console } from '@woowacourse/mission-utils'; +import { numOfLotto } from '../src/InputComponent.js'; + class App { - async run() {} + async run() { + try { + // 로또 구입금액 입력 + Console.print('구입금액을 입력해 주세요.'); + const price = await Console.readLineAsync(''); + const lotto = numOfLotto(price); // 로또 개수 + if (lotto == 0) this.throwError('로또 구입 금액 입력 오류'); + } catch (error) { + Console.print(error.message); + throw error; + } + } + + throwError(message) { + throw new Error(`[ERROR] ${message}`); + } } export default App; diff --git a/src/InputComponent.js b/src/InputComponent.js new file mode 100644 index 000000000..ea8b8b1e9 --- /dev/null +++ b/src/InputComponent.js @@ -0,0 +1,6 @@ +import { Console } from '@woowacourse/mission-utils'; + +export function numOfLotto(price) { + if (price % 1000 != 0) return 0; + return price / 1000; +} From 96f35542133903646e10e0341af75b9cc7f016d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 21:21:07 +0900 Subject: [PATCH 03/12] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 8 +++++++- src/InputComponent.js | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index f068b7825..4a0f07f43 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,5 @@ import { Console } from '@woowacourse/mission-utils'; -import { numOfLotto } from '../src/InputComponent.js'; +import { numOfLotto, chkSelectedNum } from '../src/InputComponent.js'; class App { async run() { @@ -9,6 +9,12 @@ class App { const price = await Console.readLineAsync(''); const lotto = numOfLotto(price); // 로또 개수 if (lotto == 0) this.throwError('로또 구입 금액 입력 오류'); + + // 당첨 번호 입력 + Console.print('\n당첨 번호를 입력해 주세요.'); + const numbers = (await Console.readLineAsync('')).split(','); + const selectedNum = chkSelectedNum(numbers); + if (!selectedNum) this.throwError('당첨 번호 입력 오류'); } catch (error) { Console.print(error.message); throw error; diff --git a/src/InputComponent.js b/src/InputComponent.js index ea8b8b1e9..fa4398acf 100644 --- a/src/InputComponent.js +++ b/src/InputComponent.js @@ -4,3 +4,12 @@ export function numOfLotto(price) { if (price % 1000 != 0) return 0; return price / 1000; } + +export function chkSelectedNum(num) { + const result = []; + for (let i = 0; i < num.length; i++) { + let trimmed = num[i].trim(); + if (trimmed !== '') result.push(trimmed); + } + if (result.length == 6) return result; +} From b2d6cc7f4539bdefcaa97702b76625fa551a260f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 21:29:42 +0900 Subject: [PATCH 04/12] =?UTF-8?q?feat:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5?= 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 4a0f07f43..2a4ff2f88 100644 --- a/src/App.js +++ b/src/App.js @@ -15,6 +15,10 @@ class App { const numbers = (await Console.readLineAsync('')).split(','); const selectedNum = chkSelectedNum(numbers); if (!selectedNum) this.throwError('당첨 번호 입력 오류'); + + // 보너스 번호 입력 + Console.print('\n보너스 번호를 입력해 주세요.'); + const bonusNum = await Console.readLineAsync(''); } catch (error) { Console.print(error.message); throw error; From 98ea54604264cea62b8484edc4a9c7615b8f7683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 21:56:00 +0900 Subject: [PATCH 05/12] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EB=BD=91=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 2a4ff2f88..cf0e4eb05 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import { Console } from '@woowacourse/mission-utils'; +import { Console, MissionUtils } from '@woowacourse/mission-utils'; import { numOfLotto, chkSelectedNum } from '../src/InputComponent.js'; class App { @@ -10,6 +10,17 @@ class App { const lotto = numOfLotto(price); // 로또 개수 if (lotto == 0) this.throwError('로또 구입 금액 입력 오류'); + // 로또 구매 + const lottoArr = []; + Console.print(`\n${lotto}개를 구매했습니다.`); + for (let i = 0; i < lotto; i++) { + const sortedNumbers = MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b); + lottoArr.push(sortedNumbers); + } + for (let i = 0; i < lotto; i++) { + Console.print(lottoArr[i]); + } + // 당첨 번호 입력 Console.print('\n당첨 번호를 입력해 주세요.'); const numbers = (await Console.readLineAsync('')).split(','); From 823a8e94762c4c7e9671680900c8b47e6adb79bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 22:29:14 +0900 Subject: [PATCH 06/12] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 4 ++++ src/Lotto.js | 20 ++++++++++---------- src/VerifyLotto.js | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 src/VerifyLotto.js diff --git a/src/App.js b/src/App.js index cf0e4eb05..8fada1f02 100644 --- a/src/App.js +++ b/src/App.js @@ -30,6 +30,10 @@ class App { // 보너스 번호 입력 Console.print('\n보너스 번호를 입력해 주세요.'); const bonusNum = await Console.readLineAsync(''); + + // 당첨 통계 출력 + Console.print('\n당첨 통계'); + Console.print('\n---'); } catch (error) { Console.print(error.message); throw error; diff --git a/src/Lotto.js b/src/Lotto.js index cb0b1527e..c24dd5665 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -1,18 +1,18 @@ class Lotto { - #numbers; + #numbers; - constructor(numbers) { - this.#validate(numbers); - this.#numbers = numbers; - } + constructor(numbers) { + this.#validate(numbers); + this.#numbers = numbers; + } - #validate(numbers) { - if (numbers.length !== 6) { - throw new Error("[ERROR] 로또 번호는 6개여야 합니다."); + #validate(numbers) { + if (numbers.length !== 6) { + throw new Error('[ERROR] 로또 번호는 6개여야 합니다.'); + } } - } - // TODO: 추가 기능 구현 + // TODO: 추가 기능 구현 } export default Lotto; diff --git a/src/VerifyLotto.js b/src/VerifyLotto.js new file mode 100644 index 000000000..7d8ea7d30 --- /dev/null +++ b/src/VerifyLotto.js @@ -0,0 +1,17 @@ +export function verifyLotto(lottoArr, correctNum) { + const result = Array(7).fill(0); + + for (let i = 0; i < lottoArr.length; i++) { + let outcome = numOfCorrected(lottoArr[i], correctNum); + if (outcome > 2) result[outcome] += 1; + } + return result; +} + +function numOfCorrected(nums, correctNum) { + let cnt = 0; + for (let i = 0; i < 6; i++) { + if (nums.includes(correctNum[i])) cnt++; + } + return cnt; +} From ffadce50a7105592e8fdb0b2f95c65cd9f321f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 22:38:09 +0900 Subject: [PATCH 07/12] =?UTF-8?q?feat:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EB=A5=BC=20=ED=8F=AC=ED=95=A8=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A1=9C=EB=98=90=20=ED=99=95=EC=9D=B8=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/VerifyLotto.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/VerifyLotto.js b/src/VerifyLotto.js index 7d8ea7d30..6d662c458 100644 --- a/src/VerifyLotto.js +++ b/src/VerifyLotto.js @@ -1,8 +1,13 @@ -export function verifyLotto(lottoArr, correctNum) { +export function verifyLotto(lottoArr, correctNum, bonus) { const result = Array(7).fill(0); for (let i = 0; i < lottoArr.length; i++) { let outcome = numOfCorrected(lottoArr[i], correctNum); + // 보너스 번호를 포함하는 경우 + if (outcome == 4 && chkBonusLotto(lottoArr[i], bonus)) { + result[2] += 1; + continue; + } if (outcome > 2) result[outcome] += 1; } return result; @@ -15,3 +20,10 @@ function numOfCorrected(nums, correctNum) { } return cnt; } + +function chkBonusLotto(nums, bonus) { + for (let i = 0; i < 6; i++) { + if (nums[i] == bonus) return true; + } + return false; +} From fe60a002ccf312ebe13a823d39d5b0a1dfb73f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 23:02:28 +0900 Subject: [PATCH 08/12] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EB=82=B4=EC=97=AD=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 5 ++++- src/VerifyLotto.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index 8fada1f02..8fbd7d94e 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,6 @@ import { Console, MissionUtils } from '@woowacourse/mission-utils'; import { numOfLotto, chkSelectedNum } from '../src/InputComponent.js'; +import { verifyLotto, showLottoStatistics } from '../src/VerifyLotto.js'; class App { async run() { @@ -33,7 +34,9 @@ class App { // 당첨 통계 출력 Console.print('\n당첨 통계'); - Console.print('\n---'); + Console.print('---'); + const result = verifyLotto(lottoArr, selectedNum, bonusNum); + showLottoStatistics(result); } catch (error) { Console.print(error.message); throw error; diff --git a/src/VerifyLotto.js b/src/VerifyLotto.js index 6d662c458..cdce3b078 100644 --- a/src/VerifyLotto.js +++ b/src/VerifyLotto.js @@ -1,10 +1,12 @@ +import { Console } from '@woowacourse/mission-utils'; + export function verifyLotto(lottoArr, correctNum, bonus) { const result = Array(7).fill(0); for (let i = 0; i < lottoArr.length; i++) { let outcome = numOfCorrected(lottoArr[i], correctNum); // 보너스 번호를 포함하는 경우 - if (outcome == 4 && chkBonusLotto(lottoArr[i], bonus)) { + if (outcome == 5 && chkBonusLotto(lottoArr[i], bonus)) { result[2] += 1; continue; } @@ -27,3 +29,11 @@ function chkBonusLotto(nums, bonus) { } return false; } + +export function showLottoStatistics(arr) { + Console.print(`3개 일치 (5,000원) - ${arr[3]}`); + Console.print(`4개 일치 (50,000원) - ${arr[4]}`); + Console.print(`5개 일치 (1,500,000원) - ${arr[5]}`); + Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${arr[2]}`); + Console.print(`6개 일치 (2,000,000,000원) - ${arr[6]}`); +} From d75b50e425dcc50c5f6e1587cbfadd5d445705e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 23:24:49 +0900 Subject: [PATCH 09/12] =?UTF-8?q?fix:=20=EC=9E=85=EB=A0=A5=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EC=A0=95=EC=88=98=ED=98=95=20=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 10 +++++++--- src/Lotto.js | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/App.js b/src/App.js index 8fbd7d94e..7265512ff 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,7 @@ import { Console, MissionUtils } from '@woowacourse/mission-utils'; import { numOfLotto, chkSelectedNum } from '../src/InputComponent.js'; import { verifyLotto, showLottoStatistics } from '../src/VerifyLotto.js'; +import Lotto from './Lotto.js'; class App { async run() { @@ -16,7 +17,8 @@ class App { Console.print(`\n${lotto}개를 구매했습니다.`); for (let i = 0; i < lotto; i++) { const sortedNumbers = MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b); - lottoArr.push(sortedNumbers); + const lottoInstance = new Lotto(sortedNumbers); + lottoArr.push(lottoInstance.getNumbers()); } for (let i = 0; i < lotto; i++) { Console.print(lottoArr[i]); @@ -25,12 +27,14 @@ class App { // 당첨 번호 입력 Console.print('\n당첨 번호를 입력해 주세요.'); const numbers = (await Console.readLineAsync('')).split(','); - const selectedNum = chkSelectedNum(numbers); + let selectedNum = chkSelectedNum(numbers); if (!selectedNum) this.throwError('당첨 번호 입력 오류'); + const selectedNumInstance = new Lotto(selectedNum); + selectedNum = selectedNumInstance.getNumbers(); // 보너스 번호 입력 Console.print('\n보너스 번호를 입력해 주세요.'); - const bonusNum = await Console.readLineAsync(''); + const bonusNum = parseInt(await Console.readLineAsync('')); // 당첨 통계 출력 Console.print('\n당첨 통계'); diff --git a/src/Lotto.js b/src/Lotto.js index c24dd5665..3c6e4a16e 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -13,6 +13,15 @@ class Lotto { } // TODO: 추가 기능 구현 + // 정수형으로 변환 + #changeToInt() { + this.#numbers = this.#numbers.map((num) => parseInt(num, 10)); + } + + getNumbers() { + this.#changeToInt(this.#numbers); + return this.#numbers; + } } export default Lotto; From d370649df5525abf7284b51542ef6b8e34388cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 23:34:55 +0900 Subject: [PATCH 10/12] =?UTF-8?q?feat:=20=EC=88=98=EC=9D=B5=EB=A5=A0=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=20=EB=B0=8F=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 6 +++++- src/VerifyLotto.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 7265512ff..45244fa84 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,6 @@ import { Console, MissionUtils } from '@woowacourse/mission-utils'; import { numOfLotto, chkSelectedNum } from '../src/InputComponent.js'; -import { verifyLotto, showLottoStatistics } from '../src/VerifyLotto.js'; +import { verifyLotto, showLottoStatistics, CalculateRate } from '../src/VerifyLotto.js'; import Lotto from './Lotto.js'; class App { @@ -41,6 +41,10 @@ class App { Console.print('---'); const result = verifyLotto(lottoArr, selectedNum, bonusNum); showLottoStatistics(result); + + // 수익률 계산 및 출력 + const gainRate = CalculateRate(parseInt(price), result); + Console.print(`총 수익률은 ${gainRate}%입니다.`); } catch (error) { Console.print(error.message); throw error; diff --git a/src/VerifyLotto.js b/src/VerifyLotto.js index cdce3b078..cda11c2b2 100644 --- a/src/VerifyLotto.js +++ b/src/VerifyLotto.js @@ -37,3 +37,13 @@ export function showLottoStatistics(arr) { Console.print(`5개 일치, 보너스 볼 일치 (30,000,000원) - ${arr[2]}`); Console.print(`6개 일치 (2,000,000,000원) - ${arr[6]}`); } + +export function CalculateRate(pay, arr) { + let total = 0; + if (arr[2] != 0) total += 30000000 * arr[2]; + if (arr[3] != 0) total += 5000 * arr[3]; + if (arr[4] != 0) total += 50000 * arr[4]; + if (arr[5] != 0) total += 1500000 * arr[5]; + if (arr[6] != 0) total += 2000000000 * arr[6]; + return total / pay; +} From 0e5e3416056c128ac2c9a60e6cb2fe365d1b7012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 23:42:42 +0900 Subject: [PATCH 11/12] =?UTF-8?q?feat:=20=EB=B2=94=EC=9C=84=20=EC=B4=88?= =?UTF-8?q?=EA=B3=BC=ED=95=98=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 1 + src/Lotto.js | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/App.js b/src/App.js index 45244fa84..196156fd6 100644 --- a/src/App.js +++ b/src/App.js @@ -30,6 +30,7 @@ class App { let selectedNum = chkSelectedNum(numbers); if (!selectedNum) this.throwError('당첨 번호 입력 오류'); const selectedNumInstance = new Lotto(selectedNum); + selectedNumInstance.validateRange(); selectedNum = selectedNumInstance.getNumbers(); // 보너스 번호 입력 diff --git a/src/Lotto.js b/src/Lotto.js index 3c6e4a16e..166fc5e4f 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -13,6 +13,15 @@ class Lotto { } // TODO: 추가 기능 구현 + // 로또 번호가 1부터 45 사이의 숫자가 아닌 경우 + validateRange() { + for (let i = 0; i < 6; i++) { + if (this.#numbers[i] < 1 || this.#numbers[i] > 45) { + throw new Error('[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.'); + } + } + } + // 정수형으로 변환 #changeToInt() { this.#numbers = this.#numbers.map((num) => parseInt(num, 10)); From 2c27f23f98f704e38e556a43934dfce81ea2b5d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9C=ED=98=84?= Date: Mon, 4 Nov 2024 23:53:13 +0900 Subject: [PATCH 12/12] =?UTF-8?q?feat:=20=EC=A4=91=EB=B3=B5=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=EC=88=AB=EC=9E=90=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Lotto.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Lotto.js b/src/Lotto.js index 166fc5e4f..76033890b 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -10,6 +10,7 @@ class Lotto { if (numbers.length !== 6) { throw new Error('[ERROR] 로또 번호는 6개여야 합니다.'); } + this.validateDuplicate(numbers); } // TODO: 추가 기능 구현 @@ -17,11 +18,18 @@ class Lotto { validateRange() { for (let i = 0; i < 6; i++) { if (this.#numbers[i] < 1 || this.#numbers[i] > 45) { - throw new Error('[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.'); } } } + // 중복되는 숫자가 있는 경우 + validateDuplicate(numbers) { + const uniqueNumbers = new Set(numbers); + if (uniqueNumbers.size !== numbers.length) { + throw new Error('[ERROR] 로또 번호는 중복되지 않는 숫자여야 합니다.'); + } + } + // 정수형으로 변환 #changeToInt() { this.#numbers = this.#numbers.map((num) => parseInt(num, 10));