From 75e54abe6e6c46d160e239b8cbd06960b80d4567 Mon Sep 17 00:00:00 2001 From: GeumZu Date: Fri, 7 Feb 2025 03:03:43 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EA=B3=84=EC=82=B0=EA=B8=B0=20=EC=B2=AB?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 091aa0a..842fbcb 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,48 @@ +import { Console } from "@woowacourse/mission-utils"; + class App { - async run() {} + async run() { + Console.print("") + } + + async play() { + + try { + const input = await Console.readLineAsync("덧셈할 문자열을 입력해 주세요.\n"); + + const trimmedInput = input.trim(); + const result = this.calculate(trimmedInput); + Console.print(`결과 : ${result}`); + } catch (error) { + Console.print(error.message); + } + + } + + calculate(input) { + if (!input) return 0; + + let delimiter = /[,:]/; + const customDelimeterMatch = input.match(/^\/\/(.)\n(.*)/); + + if (customDelimeterMatch) { + delimiter = new RergExp(customDelimeterMatch[1]); + input = customDelimeterMatch[2]; + } + + const numbers = input.split(delimiter).map(num => { + const parsed = Number(num); + if (isNaN(parsed) || parsed < 0 ) { + throw new Error("IllegalArgumentException: 잘못된 입력값입니다."); + } + return parsed; + }); + + return numbers.reduce((sum, num) => sum + num, 0); + } } +const app = new App(); +app.play(); + export default App; From e09845920465bf95dbb1cc844b514d0217d6afaa Mon Sep 17 00:00:00 2001 From: GeumZu Date: Fri, 7 Feb 2025 20:15:33 +0900 Subject: [PATCH 2/3] =?UTF-8?q?README.md=EC=97=90=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 13420b2..8d6db62 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ -# javascript-calculator-precourse \ No newline at end of file +# javascript-calculator-precourse + +## 기능 목록 + +1. 사용자의 입력을 받아 문자열을 처리한다다 +2. 기본 구분자(쉼표 `,` 또는 콜론 `:`)를 기준으로 숫자를 추출한다. +3. 추출한 숫자들의 합을 계산하여 반환한다. +4. 커스텀 구분자 기능을 지원한다. + - 문자열 앞부분에 `//구분자\n` 형식이 포함된 경우 해당 구분자를 사용하여 숫자를 분리한다. +5. 입력값이 비어있을 경우 `0`을 반환한다. +6. 입력값이 숫자가 아닌 경우 예외(`IllegalArgumentException`)를 발생시킨다. +7. `@woowacourse/mission-utils`의 `Console.readLineAsync`를 사용하여 입력을 받고, `Console.print`를 사용하여 결과를 출력한다. \ No newline at end of file From 7fe90247a9698d60e5cd1ac4ba21624268df7510 Mon Sep 17 00:00:00 2001 From: Guemzu Date: Fri, 21 Feb 2025 06:36:45 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/App.js | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8d6db62..057d3e0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## 기능 목록 -1. 사용자의 입력을 받아 문자열을 처리한다다 +1. 사용자의 입력을 받아 문자열을 처리한다 2. 기본 구분자(쉼표 `,` 또는 콜론 `:`)를 기준으로 숫자를 추출한다. 3. 추출한 숫자들의 합을 계산하여 반환한다. 4. 커스텀 구분자 기능을 지원한다. diff --git a/src/App.js b/src/App.js index 842fbcb..6a7c75c 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,17 @@ import { Console } from "@woowacourse/mission-utils"; class App { - async run() { - Console.print("") - } + escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } async play() { try { const input = await Console.readLineAsync("덧셈할 문자열을 입력해 주세요.\n"); - - const trimmedInput = input.trim(); + // 사용자가 입력한 "\n"을 실제 개행 문자로 치환 + const replacedInput = input.replace(/\\n/g, "\n"); + const trimmedInput = replacedInput.trim(); const result = this.calculate(trimmedInput); Console.print(`결과 : ${result}`); } catch (error) { @@ -26,8 +27,9 @@ class App { const customDelimeterMatch = input.match(/^\/\/(.)\n(.*)/); if (customDelimeterMatch) { - delimiter = new RergExp(customDelimeterMatch[1]); - input = customDelimeterMatch[2]; + const escapedDelimiter = this.escapeRegExp(customDelimeterMatch[1]) + delimiter = new RegExp(escapedDelimiter); + input = customDelimeterMatch[2]; } const numbers = input.split(delimiter).map(num => {