From 0bb685026ee0cdca598175a981281ae80a705dfd Mon Sep 17 00:00:00 2001 From: dozin Date: Mon, 20 Oct 2025 01:23:45 +0900 Subject: [PATCH 1/7] =?UTF-8?q?docs:=20=EA=B5=AC=ED=98=84=ED=95=A0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20README=EC=97=90=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 13420b29..af6e06f2 100644 --- a/README.md +++ b/README.md @@ -1 +1,19 @@ -# javascript-calculator-precourse \ No newline at end of file +# javascript-calculator-precourse + +## 기능 요구 사항 + +1. 입력 문자열 파싱 및 기본 처리 + +- 입력 문자열이 빈 문자열이거나 null인 경우에 0을 반환한다. +- 입력 문자열에 숫자 하나만 있는 경우 해당 숫자를 반환한다. +- 문자열에 포함된 쉼표 또는 콜론을 구분자로 사용하여 숫자를 분리한다. + +2. 커스텀 구분자 처리 + +- 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 정한다. +- 커스텀 구분자를 사용해 숫자를 분리하고 합을 계산한다. + +3. 예외처리 + +- 음수가 포함된 경우 "[error]" 메시지와 함께 error를 발생시키고 애플리케이션을 종료한다. +- 숫자가 아닌 문자가 포함될 경우 [error]메시지와 함께 error를 발생시키고 애플리케이션을 종료한다. From 94551703ecc6eaa5fd5a48b586110611622b6762 Mon Sep 17 00:00:00 2001 From: dozin Date: Mon, 20 Oct 2025 02:12:55 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat=20:=20=EA=B8=B0=EB=B3=B8=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=EC=9E=90=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EB=8D=A7=EC=85=88=20=EA=B8=B0=EB=8A=A5=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/calculator.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/calculator.js diff --git a/src/calculator.js b/src/calculator.js new file mode 100644 index 00000000..d933a3b8 --- /dev/null +++ b/src/calculator.js @@ -0,0 +1,16 @@ +export function add(text) { + if (text === null || text.length === 0) { + return 0; + } + + const numbers = text.split(/[,\:]/); + let sum = 0; + for (const number of numbers) { + if (number === "") continue; + const num = Number(number); + + sum += num; + } + + return num; +} From ae3df40157bda428b029ea41f28e89e03dc126af Mon Sep 17 00:00:00 2001 From: dozin Date: Mon, 20 Oct 2025 02:36:45 +0900 Subject: [PATCH 3/7] =?UTF-8?q?fix=20:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EB=8D=A7=EC=85=88=20=EA=B2=B0=EA=B3=BC=EA=B0=80=20=EC=98=AC?= =?UTF-8?q?=EB=B0=94=EB=A5=B4=EA=B2=8C=20=EB=B3=80=ED=99=98=EB=90=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/calculator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calculator.js b/src/calculator.js index d933a3b8..cdcd1f22 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -12,5 +12,5 @@ export function add(text) { sum += num; } - return num; + return sum; } From bdbe7d6f95b94f3091280a36ca1ef494e4c57905 Mon Sep 17 00:00:00 2001 From: dozin Date: Mon, 20 Oct 2025 02:37:41 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat=20:=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9E=85=EC=B6=9C=EB=A0=A5=20=EA=B2=B0=EA=B3=BC=20=EA=B8=B0?= =?UTF-8?q?=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/App.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 091aa0a5..64c7ecdb 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,19 @@ +import { Console } from "@woowacourse/mission-utils"; +import { add } from "./calculator.js"; + class App { - async run() {} + async run() { + try { + const input = await Console.readLineAsync("문자열을 입력해주세요.\n"); + + const result = add(input); + Console.print(`결과:${result}`); + } catch (error) { + if (error.message.startsWith("[ERROR]")) { + Console.print(error.message); + } + } + } } export default App; From e8a21b7359963223d6deb13c159253a3f3da7d7e Mon Sep 17 00:00:00 2001 From: dozin Date: Mon, 20 Oct 2025 09:14:19 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=EC=BB=A4=EC=8A=A4=ED=85=80=20?= =?UTF-8?q?=EA=B5=AC=EB=B6=84=EC=9E=90=20=EC=B2=98=EB=A6=AC=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/calculator.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index cdcd1f22..b2a679f5 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,11 +1,37 @@ export function add(text) { - if (text === null || text.length === 0) { + let input = text; + if (text && text.startsWith('"') && text.endsWith('"')) { + input = text.slice(1, -1); + } + + if (input && input.includes("\\n")) { + input = input.replace(/\\n/g, "\n"); + } + + if (input === null || input.length === 0) { return 0; } - const numbers = text.split(/[,\:]/); + let delimiter = /[,\:]/; + let numbers = input; + + if (input.startsWith("//")) { + const delimiterEndIndex = input.indexOf("\n"); + if (delimiterEndIndex === -1) { + throw new Error("[ERROR] 커스텀 구분자 형식이 올바르지 않습니다."); + } + const customDelimiter = input.substring(2, delimiterEndIndex); + const escapedDelimiter = customDelimiter.replace( + /[.*+?^${}()|[\]\\]/g, + "\\$&" + ); + delimiter = new RegExp(escapedDelimiter); + numbers = input.substring(delimiterEndIndex + 1); + } + + const numberArray = numbers.split(delimiter); let sum = 0; - for (const number of numbers) { + for (const number of numberArray) { if (number === "") continue; const num = Number(number); From e9746ca32d89ad8fd7d6f12737846c4692a03458 Mon Sep 17 00:00:00 2001 From: dozin Date: Mon, 20 Oct 2025 23:10:03 +0900 Subject: [PATCH 6/7] =?UTF-8?q?feat=20:=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/calculator.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/calculator.js b/src/calculator.js index b2a679f5..d9f267f6 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -34,7 +34,12 @@ export function add(text) { for (const number of numberArray) { if (number === "") continue; const num = Number(number); - + if (isNaN(num)) { + throw new Error("[ERROR] 숫자가 아닌 값이 입력되었습니다."); + } + if (num < 0) { + throw new Error("[ERROR] 음수는 입력할 수 없습니다."); + } sum += num; } From 62c06e0d400e53eb769d6829626a522794f013fb Mon Sep 17 00:00:00 2001 From: dozin Date: Mon, 20 Oct 2025 23:15:19 +0900 Subject: [PATCH 7/7] =?UTF-8?q?fix=20:=20=EC=B6=9C=EB=A0=A5=20=ED=98=95?= =?UTF-8?q?=EC=8B=9D=20=EB=B0=8F=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/App.js b/src/App.js index 64c7ecdb..6f0ad7f1 100644 --- a/src/App.js +++ b/src/App.js @@ -3,16 +3,11 @@ import { add } from "./calculator.js"; class App { async run() { - try { - const input = await Console.readLineAsync("문자열을 입력해주세요.\n"); - - const result = add(input); - Console.print(`결과:${result}`); - } catch (error) { - if (error.message.startsWith("[ERROR]")) { - Console.print(error.message); - } - } + const input = await Console.readLineAsync( + "덧셈할 문자열을 입력해 주세요.\n" + ); + const result = add(input); + Console.print(`결과 : ${result}`); } }