From 958926b3d7cfbe1104abfaf09a77c17d4c6db654 Mon Sep 17 00:00:00 2001 From: kimywann Date: Mon, 20 Oct 2025 20:43:03 +0900 Subject: [PATCH 1/6] =?UTF-8?q?docs:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EB=8D=A7=EC=85=88=20=EA=B3=84=EC=82=B0=EA=B8=B0=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 --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 13420b29..2e190c8e 100644 --- a/README.md +++ b/README.md @@ -1 +1,22 @@ -# javascript-calculator-precourse \ No newline at end of file +# javascript-calculator-precourse + +## 기능 요구 사항 + +문자열 덧셈 계산기를 구현한다. 입력한 문자열에 포함된 숫자를 모두 더한 결과를 반환한다. + +### 기본 규칙 + +- 빈 문자열 입력 시 0을 반환한다. +- 쉼표(,) 또는 콜론(:)을 구분자로 사용하여 숫자를 분리할 수 있다. + +### 커스텀 구분자 + +- `//`와 `\n` 사이에 위치하는 문자를 커스텀 구분자로 사용할 수 있다. + +### 예외 처리 + +- 사용자가 잘못된 값을 입력한 경우 `"[ERROR]"`로 시작하는 메시지를 출력하고 `Error`를 발생시킨 후 프로그램을 종료한다. +- 잘못된 값 예시 + - 음수 입력 + - 숫자가 아닌 값 입력 + - 구분자 규칙을 따르지 않은 입력 등 From 3f10ffb746804e235e1a374beb5c6ae070ad65bc Mon Sep 17 00:00:00 2001 From: kimywann Date: Mon, 20 Oct 2025 22:26:17 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat(App):=20=EC=8B=A4=ED=96=89=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B0=8F=20=EC=9E=85=EB=A0=A5-=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=20=EA=B5=AC=EC=A1=B0=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 | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 091aa0a5..c4b0a4f2 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,22 @@ +import { Console } from "@woowacourse/mission-utils"; + +import IsInputValidate from "./Validate.js"; +import { calculateSumOfNumbers } from "./calculator.js"; + class App { - async run() {} + async run() { + const inputs = await Console.readLineAsync( + "덧셈할 문자열을 입력해 주세요.\n" + ); + + const numbers = parseInput(inputs); + IsInputValidate(numbers); + + const sum = calculateSumOfNumbers(numbers); + Console.print(`결과 : ${sum}`); + + return; + } } export default App; From e27bfdd1341c778278e4f4f1b8e66c095ee7b38a Mon Sep 17 00:00:00 2001 From: kimywann Date: Mon, 20 Oct 2025 22:26:55 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat(inputParser):=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4=20=EC=9E=85=EB=A0=A5=20=ED=8C=8C=EC=8B=B1=20=EB=B0=8F?= =?UTF-8?q?=20=EC=BB=A4=EC=8A=A4=ED=85=80=20=EA=B5=AC=EB=B6=84=EC=9E=90=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/inputParser.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/inputParser.js diff --git a/src/inputParser.js b/src/inputParser.js new file mode 100644 index 00000000..2ce2d624 --- /dev/null +++ b/src/inputParser.js @@ -0,0 +1,21 @@ +const parseInput = (inputs) => { + const formattedInput = inputs.replace(/\\n/g, "\n"); + + let numbers; + + if (formattedInput.startsWith("//")) { + const delimiterEndIndex = formattedInput.indexOf("\n"); + const customDelimiter = formattedInput.slice(2, delimiterEndIndex); + const numbersString = formattedInput.slice(delimiterEndIndex + 1); + + numbers = numbersString + .split(customDelimiter) + .flatMap((part) => part.split(",").flatMap((p) => p.split(":"))); + } else { + numbers = inputs.split(/[,:]/); + } + + return numbers; +}; + +export default parseInput; From e7cf19ea371dbd174f6dd53afaa98939a66f04ac Mon Sep 17 00:00:00 2001 From: kimywann Date: Mon, 20 Oct 2025 22:28:00 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat(validate):=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/validate.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/validate.js diff --git a/src/validate.js b/src/validate.js new file mode 100644 index 00000000..73b87d73 --- /dev/null +++ b/src/validate.js @@ -0,0 +1,12 @@ +const IsInputValidate = (numbers) => { + numbers.forEach((number) => { + if (number === "") { + throw new Error("[ERROR] 빈 문자열은 입력할 수 없습니다."); + } + if (number <= 0) { + throw new Error("[ERROR] 양수만 입력할 수 있습니다."); + } + }); +}; + +export default IsInputValidate; From 2b1a75d30b26a30c611e2387bbcbf8d372837dba Mon Sep 17 00:00:00 2001 From: kimywann Date: Mon, 20 Oct 2025 22:28:36 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat(calculator):=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EB=B0=9B=EC=9D=80=20=EC=88=AB=EC=9E=90=20=ED=95=A9=EC=82=B0=20?= =?UTF-8?q?=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/calculator.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/calculator.js diff --git a/src/calculator.js b/src/calculator.js new file mode 100644 index 00000000..852acb46 --- /dev/null +++ b/src/calculator.js @@ -0,0 +1,5 @@ +export const calculateSumOfNumbers = (numbers) => { + return numbers.reduce((acc, curr) => { + return acc + parseInt(curr); + }, 0); +}; From 088f62428ad3785a8f00e75ea699d05e3c8b158f Mon Sep 17 00:00:00 2001 From: kimywann Date: Mon, 20 Oct 2025 22:43:42 +0900 Subject: [PATCH 6/6] =?UTF-8?q?fix(App):=20parseInput=20=EB=88=84=EB=9D=BD?= =?UTF-8?q?=20=EC=9E=84=ED=8F=AC=ED=8A=B8=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20validate=20=EB=AA=A8=EB=93=88=20=EC=86=8C=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index c4b0a4f2..874282d7 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,7 @@ import { Console } from "@woowacourse/mission-utils"; -import IsInputValidate from "./Validate.js"; +import parseInput from "./inputParser.js"; +import IsInputValidate from "./validate.js"; import { calculateSumOfNumbers } from "./calculator.js"; class App {