From ec4836f0a260d4a6811aa5a800371e6f92a92a61 Mon Sep 17 00:00:00 2001 From: ahhyun Date: Fri, 18 Oct 2024 16:01:26 +0900 Subject: [PATCH 01/11] =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EC=A0=84=20?= =?UTF-8?q?=EB=AC=B8=EA=B5=AC=20=EC=B6=9C=EB=A0=A5=20=EB=B0=8F=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EA=B0=92=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/App.js b/src/App.js index 091aa0a5d..afcdd0ed9 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,15 @@ +import { Console } from '@woowacourse/mission-utils'; + class App { - async run() {} + async run() { + const input = await Console.readLineAsync('입력 값을 입력하세요: '); + var number = input.split(',').map(Number); + var sum = 0; + for (let i=0; i Date: Fri, 18 Oct 2024 16:04:52 +0900 Subject: [PATCH 02/11] =?UTF-8?q?=EC=89=BC=ED=91=9C(,)=EC=9D=98=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=9D=80=20?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=98=20=EC=88=AB=EC=9E=90=20?= =?UTF-8?q?=ED=95=A9=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/App.js b/src/App.js index afcdd0ed9..c48dd0194 100644 --- a/src/App.js +++ b/src/App.js @@ -8,7 +8,6 @@ class App { for (let i=0; i Date: Fri, 18 Oct 2024 16:09:46 +0900 Subject: [PATCH 03/11] =?UTF-8?q?=EC=BD=9C=EB=A1=A0(:)=EC=9D=98=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=EB=8F=84=20=ED=8F=AC=ED=95=A8?= 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 c48dd0194..46948d111 100644 --- a/src/App.js +++ b/src/App.js @@ -3,11 +3,12 @@ import { Console } from '@woowacourse/mission-utils'; class App { async run() { const input = await Console.readLineAsync('입력 값을 입력하세요: '); - var number = input.split(',').map(Number); + var number = input.split(/[,|:]/).map(Number); var sum = 0; for (let i=0; i Date: Fri, 18 Oct 2024 23:57:12 +0900 Subject: [PATCH 04/11] =?UTF-8?q?=EC=BB=A4=EC=8A=A4=ED=85=80=EA=B3=BC=20?= =?UTF-8?q?=EC=9D=BC=EB=B0=98=20=EA=B5=AC=EB=B6=84=EC=9E=90=EB=A5=BC=20if?= =?UTF-8?q?=EB=AC=B8=EC=9C=BC=EB=A1=9C=20=EA=B5=AC=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/App.js b/src/App.js index 46948d111..8c7bafac7 100644 --- a/src/App.js +++ b/src/App.js @@ -2,14 +2,30 @@ import { Console } from '@woowacourse/mission-utils'; class App { async run() { - const input = await Console.readLineAsync('입력 값을 입력하세요: '); - var number = input.split(/[,|:]/).map(Number); - var sum = 0; - for (let i=0; i sum + num, 0); + } else if(input.includes(",") || input.includes(":")){ + let numbers = input.split(/[,|:]/).map(Number); + return numbers.reduce((sum, num) => sum + num, 0); + } + return 0; + } + +export default App; \ No newline at end of file From 3d5304161f05ae0da77f9a9993ee3e4c44d978a4 Mon Sep 17 00:00:00 2001 From: ahhyun Date: Sun, 20 Oct 2024 00:47:44 +0900 Subject: [PATCH 05/11] =?UTF-8?q?=EC=9E=85=EB=A0=A5=EB=AC=B8=EA=B5=AC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=ED=9B=84=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EC=9D=84=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=95=84=20=EB=B3=80?= =?UTF-8?q?=EC=88=98=EC=97=90=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/src/App.js b/src/App.js index 8c7bafac7..ffb9df53a 100644 --- a/src/App.js +++ b/src/App.js @@ -2,30 +2,10 @@ import { Console } from '@woowacourse/mission-utils'; class App { async run() { - const input = await Console.readLineAsync('덧셈할 문자열을 입력해 주세요.'); - const result = sumNumbers(input); - Console.print(`결과 : ${result}`); + const input = await Console.readLineAsync("덧셈할 문자열을 입력해 주세요.\n"); + Console.print(input); } } - function sumNumbers(input){ - - if (input === "") { - return 0; - } - - let customPattern = /\/\/(.+)\n/; - if(customPattern.test(input)){ - let match = input.match(customPattern); - let customWord = match[1]; - let numberPart = input.split('\n')[1]; - let numbers = numberPart.split(new RegExp(customWord)).map(Number) - return numbers.reduce((sum, num) => sum + num, 0); - } else if(input.includes(",") || input.includes(":")){ - let numbers = input.split(/[,|:]/).map(Number); - return numbers.reduce((sum, num) => sum + num, 0); - } - return 0; - } export default App; \ No newline at end of file From bd8951adbe6185e5519c77e662c6f47661f78c59 Mon Sep 17 00:00:00 2001 From: ahhyun Date: Sun, 20 Oct 2024 00:52:11 +0900 Subject: [PATCH 06/11] =?UTF-8?q?=EC=A0=80=EC=9E=A5=EB=90=9C=20=EB=B3=80?= =?UTF-8?q?=EC=88=98=EB=A5=BC=20=EB=A7=A4=EA=B0=9C=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=A1=9C=20=ED=95=98=EB=8A=94=20sumNumbers=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=A7=80=EC=A0=95?= 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 ffb9df53a..8022a1c85 100644 --- a/src/App.js +++ b/src/App.js @@ -6,6 +6,12 @@ class App { Console.print(input); } } + function sumNumbers(input) { + if (input === "") { + return 0; + } + +} export default App; \ No newline at end of file From 5b8fa5bbc14465ffd96e462e26eaa8dfdc44bd36 Mon Sep 17 00:00:00 2001 From: ahhyun Date: Sun, 20 Oct 2024 00:53:19 +0900 Subject: [PATCH 07/11] =?UTF-8?q?try~catch=20=EA=B5=AC=EB=AC=B8=EC=9D=84?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=97=AC=20=EC=88=AB=EC=9E=90=20?= =?UTF-8?q?=ED=95=A9=EC=82=B0=20=EA=B0=92=EC=9D=84=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=ED=95=98=EA=B3=A0,=20=EC=97=90=EB=9F=AC=20=EB=B0=9C=EC=83=9D?= =?UTF-8?q?=EC=8B=9C=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=ED=9B=84=20=EC=97=90=EB=9F=AC=20=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/App.js b/src/App.js index 8022a1c85..91802d5e7 100644 --- a/src/App.js +++ b/src/App.js @@ -2,14 +2,21 @@ import { Console } from '@woowacourse/mission-utils'; class App { async run() { + try { const input = await Console.readLineAsync("덧셈할 문자열을 입력해 주세요.\n"); - Console.print(input); + const result = sumNumbers(input); + Console.print(`결과 : ${result}`); + } catch (error) { + Console.print(error.message); + throw error; + } } } - function sumNumbers(input) { - if (input === "") { - return 0; - } + +function sumNumbers(input) { + if (input === "") { + return 0; + } } From db969350446c94863ca0a1afd8145d2ff462b09a Mon Sep 17 00:00:00 2001 From: ahhyun Date: Sun, 20 Oct 2024 00:54:18 +0900 Subject: [PATCH 08/11] =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=9D=80?= =?UTF-8?q?=20=EA=B2=BD=EC=9A=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/App.js b/src/App.js index 91802d5e7..9d438952b 100644 --- a/src/App.js +++ b/src/App.js @@ -20,5 +20,4 @@ function sumNumbers(input) { } - export default App; \ No newline at end of file From a461d1ac94293b57d7474a161743eea97ef3774e Mon Sep 17 00:00:00 2001 From: ahhyun Date: Sun, 20 Oct 2024 00:56:36 +0900 Subject: [PATCH 09/11] =?UTF-8?q?=EC=BB=A4=EC=8A=A4=ED=85=80=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=EC=9E=90=EB=A5=BC=20=EA=B0=80=EC=A7=80=EB=8A=94=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0(//=EB=A1=9C=20=EC=8B=9C=EC=9E=91=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B2=BD=EC=9A=B0=EB=A1=9C=20=ED=95=9C=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/App.js b/src/App.js index 9d438952b..e152f53dc 100644 --- a/src/App.js +++ b/src/App.js @@ -17,7 +17,10 @@ function sumNumbers(input) { if (input === "") { return 0; } + input = input.replace(/\\n/g, "\n"); + if (input.startsWith("//")) { + const customEndIndex = input.indexOf("\n"); } export default App; \ No newline at end of file From 97fe02d4f67b3578d1751a24cdd6cfc2ab2c0040 Mon Sep 17 00:00:00 2001 From: ahhyun Date: Sun, 20 Oct 2024 00:57:30 +0900 Subject: [PATCH 10/11] =?UTF-8?q?(=EC=97=90=EB=9F=AC=201:=20\n=EC=9D=B4=20?= =?UTF-8?q?=EC=A1=B4=EC=9E=AC=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0)?= 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 e152f53dc..5220bb876 100644 --- a/src/App.js +++ b/src/App.js @@ -21,6 +21,10 @@ function sumNumbers(input) { if (input.startsWith("//")) { const customEndIndex = input.indexOf("\n"); + + if (delimiterEndIndex === -1) { + throw new Error("[ERROR] 유효하지 않은 구분자 형식입니다."); + } } export default App; \ No newline at end of file From fe9380f0f6b0ad78956849d883fa79bad88bdfd0 Mon Sep 17 00:00:00 2001 From: ahhyun Date: Mon, 21 Oct 2024 09:53:51 +0900 Subject: [PATCH 11/11] =?UTF-8?q?=EC=B5=9C=EC=A2=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index 5220bb876..9d8900095 100644 --- a/src/App.js +++ b/src/App.js @@ -8,7 +8,7 @@ class App { Console.print(`결과 : ${result}`); } catch (error) { Console.print(error.message); - throw error; + throw error; } } } @@ -17,14 +17,45 @@ function sumNumbers(input) { if (input === "") { return 0; } + + // 입력에서 "\\n"을 줄바꿈 문자로 변환 input = input.replace(/\\n/g, "\n"); if (input.startsWith("//")) { - const customEndIndex = input.indexOf("\n"); + const delimiterEndIndex = input.indexOf("\n"); if (delimiterEndIndex === -1) { throw new Error("[ERROR] 유효하지 않은 구분자 형식입니다."); } + + const customDelimiter = input.substring(2, delimiterEndIndex); + + if (!customDelimiter) { + throw new Error("[ERROR] 유효하지 않은 구분자입니다."); + } + + const numberPart = input.substring(delimiterEndIndex + 1); + const numbers = numberPart.split(new RegExp(`[${customDelimiter},:]`)).map(validateNumber); + return numbers.reduce((sum, num) => sum + num, 0); + } + + + if (input.includes(",") || input.includes(":")) { + const numbers = input.split(/[,|:]/).map(validateNumber); + return numbers.reduce((sum, num) => sum + num, 0); + } + + throw new Error("[ERROR] 입력에 구분자가 포함되어야 합니다."); +} + +function validateNumber(value) { + const num = Number(value); + + if (num < 0) { + throw new Error("[ERROR] 음수는 입력할 수 없습니다."); + } + + return num; } export default App; \ No newline at end of file