From 0994106f898e58795540f910a8f2f584d8231ae1 Mon Sep 17 00:00:00 2001 From: ParkHyeonkyu <162525426+ParkHyeonkyu@users.noreply.github.com> Date: Tue, 4 Feb 2025 23:40:43 +0900 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EC=9A=94=EA=B5=AC=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Console을 활용하여 사용자에게 문자열을 입력받는 기능 구현 commit 테스트 --- README.md | 10 +++++++++- src/App.js | 9 +++++++-- src/practice.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/practice.js diff --git a/README.md b/README.md index 13420b2..55c0fef 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# javascript-calculator-precourse \ No newline at end of file +# javascript-calculator-precourse + +## 기능 + + 1. 문자열 입력을 요구하는 기능 + 2. 유효한(기본, 커스텀) 구분자를 기준으로 숫자를 추출하고 더하는 기능 + 쉼표(,)와 콜론(:)은 기본 구분자로, //와 \n 사이에 위치하는 문자는 커스텀 구분자로 지정 + 3. 커스텀 구분자를 설정하는 기능 + 4. 예외 처리(숫자 이외의 문자, 유효하지 않은 구분자 사용, 소수점 사용 등) \ No newline at end of file diff --git a/src/App.js b/src/App.js index 091aa0a..2b14f88 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,10 @@ +import { Console } from "@woowacourse/mission-utils"; + class App { - async run() {} + async run() { + const userInput = await Console.readLineAsync(`덧셈할 문자열을 입력해주세요.\n`); + Console.print("값: " + userInput); + } } -export default App; +export default App; \ No newline at end of file diff --git a/src/practice.js b/src/practice.js new file mode 100644 index 0000000..9476f51 --- /dev/null +++ b/src/practice.js @@ -0,0 +1,53 @@ +const userInput = await Console.readLineAsync("덧셈할 문자열을 입력해 주세요."); +Console.print("값: " + userInput); + + +const userInput2 = await Console.readLineAsync("덧셈할 두 번째 문자열을 입력해 주세요."); +Console.print("값: " + userInput2); + + +class Animal { + constructor(name) { //생성자 + this.name = name; + } + + speak() { //메서드 + console.log(`${this.name} makes a sound`); + } +} + +//클래스의 상속 +class Dog extends Animal { + constructor(name, breed){ + super(name); //super -> 부모 클래스의 생성자 호출출 + this.breed = breed; + } + + speak() { + console.log(`${this.name}은 자식 클래스이다.`); + } +} + +const dog = new Dog('푸딩', '디저트') +dog.speak(); + +//Getter와 Setter +class Circle{ + constructor(radius) { + this.radius = radius; + } + + get diameter() { + return this.radius * 2; + } + + set diameter(value){ + this.radius = value / 2; + } +} + +const circle = new Circle(5); +console.log(circle.diameter); +circle.diameter = 40; +console.log(circle.diameter); +console.log(circle.radius); From 7336ea794ed44161110f15ca0569615b1099cb05 Mon Sep 17 00:00:00 2001 From: ParkHyeonkyu <162525426+ParkHyeonkyu@users.noreply.github.com> Date: Wed, 5 Feb 2025 13:28:40 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20=EA=B8=B0=EB=B3=B8=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=EC=9E=90=20=EB=8D=A7=EC=85=88=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit split과 reduce를 활용하여 기본 구분자(,:)를 기준으로 문자열 덧셈 구현 --- README.md | 10 ++++++++-- src/App.js | 25 ++++++++++++++++++++++++- src/index.js | 2 +- src/practice.js | 5 +++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 55c0fef..5734b9c 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,11 @@ 1. 문자열 입력을 요구하는 기능 2. 유효한(기본, 커스텀) 구분자를 기준으로 숫자를 추출하고 더하는 기능 쉼표(,)와 콜론(:)은 기본 구분자로, //와 \n 사이에 위치하는 문자는 커스텀 구분자로 지정 - 3. 커스텀 구분자를 설정하는 기능 - 4. 예외 처리(숫자 이외의 문자, 유효하지 않은 구분자 사용, 소수점 사용 등) \ No newline at end of file + 2-1) 기본 구분자 구현 + 2-2) 커스텀 구분자 적용 구현 + 3. 예외 처리 + +## 예외처리 + 1. 숫자와 구분자 이외의 문자 + 2. 유효하지 않은 구분자 사용 + 3. 소수점 사용 \ No newline at end of file diff --git a/src/App.js b/src/App.js index 2b14f88..2ffac51 100644 --- a/src/App.js +++ b/src/App.js @@ -2,8 +2,31 @@ import { Console } from "@woowacourse/mission-utils"; class App { async run() { + + // 입력 값을 더해서 결과를 구함함 + const sumResult = (input) => { + + // 기본 구분자를 기준으로 배열을 만듦 + const inputArr = input.split(/[,:]/); + Console.print(inputArr); + + const sum = inputArr.reduce((acc, cur) => { + //현재 요소를 숫자로 변환하고, NaN이 아닌 경우에만 더함 + const num = Number(cur); + if (!isNaN(num)){ + return acc + num; + } + return acc; //숫자가 아니면 누적 값 유지 + }, 0); + + Console.print(sum); + return sum; + } + + // 사용자로부터 문자열을 입력 받음 const userInput = await Console.readLineAsync(`덧셈할 문자열을 입력해주세요.\n`); - Console.print("값: " + userInput); + const result = sumResult(userInput) + Console.print("값: " + result); } } diff --git a/src/index.js b/src/index.js index 02a1d38..100dd7d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ import App from "./App.js"; const app = new App(); -await app.run(); +await app.run(); \ No newline at end of file diff --git a/src/practice.js b/src/practice.js index 9476f51..e0e847c 100644 --- a/src/practice.js +++ b/src/practice.js @@ -51,3 +51,8 @@ console.log(circle.diameter); circle.diameter = 40; console.log(circle.diameter); console.log(circle.radius); + + +// 문자열에 API 적용하기 +// 1. String Methods 사용 +// 2. 문자열을 배열로 변환 후 Array Methods 사용 From 1529d7edaeddfb200d87131e50327ec2b061efbb Mon Sep 17 00:00:00 2001 From: ParkHyeonkyu <162525426+ParkHyeonkyu@users.noreply.github.com> Date: Wed, 5 Feb 2025 16:22:56 +0900 Subject: [PATCH 3/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=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit //과 \n사이에 구분자를 커스텀 구분자로 설정하는 기능 구현 --- src/App.js | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/App.js b/src/App.js index 2ffac51..b289221 100644 --- a/src/App.js +++ b/src/App.js @@ -2,32 +2,49 @@ import { Console } from "@woowacourse/mission-utils"; class App { async run() { - - // 입력 값을 더해서 결과를 구함함 const sumResult = (input) => { + Console.print("입력값 (JSON): " + JSON.stringify(input)); + + input = input.replace(/\\n/g, "\n"); // 문자열 "\n"을 개행 문자로 변환 + Console.print("변환된 입력값: " + JSON.stringify(input)); + + let delimiter = /[,:]/; + + const escapeRegExp = (str) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); + + if (input.startsWith("//")) { + const delimiterEndIndex = input.search(/\n/); // 개행 문자 찾기 + Console.print("개행 문자 인덱스: " + delimiterEndIndex); + if (delimiterEndIndex === -1) { + throw new Error("올바른 형식이 아닙니다."); // 개행이 없으면 에러 처리 + } - // 기본 구분자를 기준으로 배열을 만듦 - const inputArr = input.split(/[,:]/); + let customDelimiter = input.slice(2, delimiterEndIndex); + Console.print("커스텀 구분자 확인: " + customDelimiter); + + input = input.slice(delimiterEndIndex + 1); + Console.print("문자열 확인: " + input); + + delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); + } + + const inputArr = input.split(delimiter); Console.print(inputArr); const sum = inputArr.reduce((acc, cur) => { - //현재 요소를 숫자로 변환하고, NaN이 아닌 경우에만 더함 const num = Number(cur); - if (!isNaN(num)){ - return acc + num; - } - return acc; //숫자가 아니면 누적 값 유지 + return !isNaN(num) ? acc + num : acc; }, 0); - + Console.print(sum); + Console.print(delimiter); return sum; - } + }; - // 사용자로부터 문자열을 입력 받음 const userInput = await Console.readLineAsync(`덧셈할 문자열을 입력해주세요.\n`); - const result = sumResult(userInput) - Console.print("값: " + result); + const output = sumResult(userInput); + Console.print("값: " + output); } } -export default App; \ No newline at end of file +export default App; From 3bd762c1a496f5d23c5fb36eb5fdc948e29d87b4 Mon Sep 17 00:00:00 2001 From: ParkHyeonkyu <162525426+ParkHyeonkyu@users.noreply.github.com> Date: Thu, 6 Feb 2025 17:13:39 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat:=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit try-catch문을 활용해서 예상되는 5~6개의 예외를 처리하는 코드 구현 --- README.md | 10 +++-- __tests__/ApplicationTest.js | 2 +- src/App.js | 87 +++++++++++++++++++++++------------- src/practice.js | 58 ------------------------ 4 files changed, 65 insertions(+), 92 deletions(-) delete mode 100644 src/practice.js diff --git a/README.md b/README.md index 5734b9c..dd872de 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ 3. 예외 처리 ## 예외처리 - 1. 숫자와 구분자 이외의 문자 - 2. 유효하지 않은 구분자 사용 - 3. 소수점 사용 \ No newline at end of file + 1. 숫자와 유효 구분자 이외의 문자(한글, 영어) + 2. 잘못된 커스텀 구분자 설정 형식 + 2-1) //로 시작해서 \n로 끝나지 않는 경우 + 2-2) 중복된 구분자를 사용하는 경우 ex) //**\n + 3. 유효하지 않은 구분자 사용 ex) //*\n1,2#3 + 4. 음수 사용 ex) -1,-2,3 + 5. 소수점을 커스텀 구분자로 사용하려는 경우 \ No newline at end of file diff --git a/__tests__/ApplicationTest.js b/__tests__/ApplicationTest.js index 7c6962d..c49a66c 100644 --- a/__tests__/ApplicationTest.js +++ b/__tests__/ApplicationTest.js @@ -40,4 +40,4 @@ describe("문자열 계산기", () => { await expect(app.run()).rejects.toThrow("[ERROR]"); }); -}); +}); \ No newline at end of file diff --git a/src/App.js b/src/App.js index b289221..004226d 100644 --- a/src/App.js +++ b/src/App.js @@ -2,48 +2,75 @@ import { Console } from "@woowacourse/mission-utils"; class App { async run() { - const sumResult = (input) => { - Console.print("입력값 (JSON): " + JSON.stringify(input)); + const sumResult = async (input) => { + try { + Console.print("입력값 (JSON): " + JSON.stringify(input)); - input = input.replace(/\\n/g, "\n"); // 문자열 "\n"을 개행 문자로 변환 - Console.print("변환된 입력값: " + JSON.stringify(input)); + input = input.replace(/\\n/g, "\n"); + Console.print("변환된 입력값(개행문자): " + JSON.stringify(input)); - let delimiter = /[,:]/; + let delimiter = /[,:]/; - const escapeRegExp = (str) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); + const escapeRegExp = (str) => + str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); - if (input.startsWith("//")) { - const delimiterEndIndex = input.search(/\n/); // 개행 문자 찾기 - Console.print("개행 문자 인덱스: " + delimiterEndIndex); - if (delimiterEndIndex === -1) { - throw new Error("올바른 형식이 아닙니다."); // 개행이 없으면 에러 처리 - } + if (input.startsWith("//")) { + const delimiterEndIndex = input.search(/\n/); + Console.print("개행 문자 인덱스: " + delimiterEndIndex); + if (delimiterEndIndex === -1) { + throw new Error("\\n이 필요합니다."); + } - let customDelimiter = input.slice(2, delimiterEndIndex); - Console.print("커스텀 구분자 확인: " + customDelimiter); + let customDelimiter = input.slice(2, delimiterEndIndex); + if (/(.)\1+/.test(customDelimiter)) { + throw new Error("중복된 구분자를 사용할 수 없습니다."); + } - input = input.slice(delimiterEndIndex + 1); - Console.print("문자열 확인: " + input); + if (customDelimiter === ".") { + throw new Error(".은 구분자로 사용할 수 없습니다."); + } + Console.print("커스텀 구분자 확인: " + customDelimiter); - delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); - } + input = input.slice(delimiterEndIndex + 1); + Console.print("문자열 확인: " + input); - const inputArr = input.split(delimiter); - Console.print(inputArr); + delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); + } else if (!(input.startsWith("//") || !isNaN(Number(input[0])))) { + throw new Error("양수 또는 //로 시작해야 합니다."); + } - const sum = inputArr.reduce((acc, cur) => { - const num = Number(cur); - return !isNaN(num) ? acc + num : acc; - }, 0); + const inputArr = input.split(delimiter); + Console.print(inputArr); - Console.print(sum); - Console.print(delimiter); - return sum; + const sum = inputArr.reduce((acc, cur) => { + const num = Number(cur); + if (!isNaN(num)) { + if (num < 0) { + throw new Error("음수를 입력할 수 없습니다."); + } + return acc + num; + } + throw new Error("유효하지 않은 문자가 포함되어있습니다."); + }, 0); + + Console.print(sum); + Console.print(delimiter); + return sum; + } catch (error) { + throw error; + } }; - const userInput = await Console.readLineAsync(`덧셈할 문자열을 입력해주세요.\n`); - const output = sumResult(userInput); - Console.print("값: " + output); + try { + const userInput = await Console.readLineAsync( + `덧셈할 문자열을 입력해주세요.\n` + ); + const output = await sumResult(userInput); + Console.print("결과 : " + output); + } catch (error) { + Console.print("[ERROR]: " + error.message); + throw error; + } } } diff --git a/src/practice.js b/src/practice.js deleted file mode 100644 index e0e847c..0000000 --- a/src/practice.js +++ /dev/null @@ -1,58 +0,0 @@ -const userInput = await Console.readLineAsync("덧셈할 문자열을 입력해 주세요."); -Console.print("값: " + userInput); - - -const userInput2 = await Console.readLineAsync("덧셈할 두 번째 문자열을 입력해 주세요."); -Console.print("값: " + userInput2); - - -class Animal { - constructor(name) { //생성자 - this.name = name; - } - - speak() { //메서드 - console.log(`${this.name} makes a sound`); - } -} - -//클래스의 상속 -class Dog extends Animal { - constructor(name, breed){ - super(name); //super -> 부모 클래스의 생성자 호출출 - this.breed = breed; - } - - speak() { - console.log(`${this.name}은 자식 클래스이다.`); - } -} - -const dog = new Dog('푸딩', '디저트') -dog.speak(); - -//Getter와 Setter -class Circle{ - constructor(radius) { - this.radius = radius; - } - - get diameter() { - return this.radius * 2; - } - - set diameter(value){ - this.radius = value / 2; - } -} - -const circle = new Circle(5); -console.log(circle.diameter); -circle.diameter = 40; -console.log(circle.diameter); -console.log(circle.radius); - - -// 문자열에 API 적용하기 -// 1. String Methods 사용 -// 2. 문자열을 배열로 변환 후 Array Methods 사용 From 0ceb08737bfe6298e4a867bc11e0d2c499bce932 Mon Sep 17 00:00:00 2001 From: ParkHyeonkyu <162525426+ParkHyeonkyu@users.noreply.github.com> Date: Fri, 7 Feb 2025 10:48:15 +0900 Subject: [PATCH 5/7] =?UTF-8?q?style:=20Console=20=EC=A3=BC=EC=84=9D?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 미션 출력 조건에 맞도록 Console.print부분을 주석처리 --- src/App.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/App.js b/src/App.js index 004226d..74baba0 100644 --- a/src/App.js +++ b/src/App.js @@ -3,20 +3,18 @@ import { Console } from "@woowacourse/mission-utils"; class App { async run() { const sumResult = async (input) => { - try { - Console.print("입력값 (JSON): " + JSON.stringify(input)); - + + //Console.print("입력값 (JSON): " + JSON.stringify(input)); input = input.replace(/\\n/g, "\n"); - Console.print("변환된 입력값(개행문자): " + JSON.stringify(input)); + //Console.print("변환된 입력값(개행문자): " + JSON.stringify(input)); let delimiter = /[,:]/; - const escapeRegExp = (str) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); if (input.startsWith("//")) { const delimiterEndIndex = input.search(/\n/); - Console.print("개행 문자 인덱스: " + delimiterEndIndex); + //Console.print("개행 문자 인덱스: " + delimiterEndIndex); if (delimiterEndIndex === -1) { throw new Error("\\n이 필요합니다."); } @@ -25,14 +23,12 @@ class App { if (/(.)\1+/.test(customDelimiter)) { throw new Error("중복된 구분자를 사용할 수 없습니다."); } - if (customDelimiter === ".") { throw new Error(".은 구분자로 사용할 수 없습니다."); } - Console.print("커스텀 구분자 확인: " + customDelimiter); - + //Console.print("커스텀 구분자 확인: " + customDelimiter); input = input.slice(delimiterEndIndex + 1); - Console.print("문자열 확인: " + input); + //Console.print("문자열 확인: " + input); delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); } else if (!(input.startsWith("//") || !isNaN(Number(input[0])))) { @@ -40,7 +36,7 @@ class App { } const inputArr = input.split(delimiter); - Console.print(inputArr); + //Console.print(inputArr); const sum = inputArr.reduce((acc, cur) => { const num = Number(cur); @@ -53,12 +49,10 @@ class App { throw new Error("유효하지 않은 문자가 포함되어있습니다."); }, 0); - Console.print(sum); - Console.print(delimiter); + //Console.print(sum); + //Console.print(delimiter); return sum; - } catch (error) { - throw error; - } + }; try { @@ -68,7 +62,7 @@ class App { const output = await sumResult(userInput); Console.print("결과 : " + output); } catch (error) { - Console.print("[ERROR]: " + error.message); + //Console.print("[ERROR]: " + error.message); throw error; } } From ed245d4d03cb8c0728515e5e580a8b1abebe3a4a Mon Sep 17 00:00:00 2001 From: ParkHyeonkyu <162525426+ParkHyeonkyu@users.noreply.github.com> Date: Mon, 10 Feb 2025 13:32:22 +0900 Subject: [PATCH 6/7] =?UTF-8?q?docs:=20=EC=A3=BC=EC=84=9D=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 가독성 향상을 위한 주석 제거 --- src/App.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/App.js b/src/App.js index 74baba0..e40dc8a 100644 --- a/src/App.js +++ b/src/App.js @@ -4,9 +4,7 @@ class App { async run() { const sumResult = async (input) => { - //Console.print("입력값 (JSON): " + JSON.stringify(input)); input = input.replace(/\\n/g, "\n"); - //Console.print("변환된 입력값(개행문자): " + JSON.stringify(input)); let delimiter = /[,:]/; const escapeRegExp = (str) => @@ -14,7 +12,7 @@ class App { if (input.startsWith("//")) { const delimiterEndIndex = input.search(/\n/); - //Console.print("개행 문자 인덱스: " + delimiterEndIndex); + if (delimiterEndIndex === -1) { throw new Error("\\n이 필요합니다."); } @@ -26,9 +24,7 @@ class App { if (customDelimiter === ".") { throw new Error(".은 구분자로 사용할 수 없습니다."); } - //Console.print("커스텀 구분자 확인: " + customDelimiter); input = input.slice(delimiterEndIndex + 1); - //Console.print("문자열 확인: " + input); delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); } else if (!(input.startsWith("//") || !isNaN(Number(input[0])))) { @@ -36,7 +32,6 @@ class App { } const inputArr = input.split(delimiter); - //Console.print(inputArr); const sum = inputArr.reduce((acc, cur) => { const num = Number(cur); @@ -49,8 +44,6 @@ class App { throw new Error("유효하지 않은 문자가 포함되어있습니다."); }, 0); - //Console.print(sum); - //Console.print(delimiter); return sum; }; @@ -62,7 +55,6 @@ class App { const output = await sumResult(userInput); Console.print("결과 : " + output); } catch (error) { - //Console.print("[ERROR]: " + error.message); throw error; } } From 0d424273253938663ed4825555efbf7c56924981 Mon Sep 17 00:00:00 2001 From: ParkHyeonkyu <162525426+ParkHyeonkyu@users.noreply.github.com> Date: Thu, 13 Feb 2025 23:20:30 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EB=B0=98=EC=98=81=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 깃허브 코드리뷰의 피드백을 반영한 리팩토링 코드입니당 --- src/App.js | 68 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/App.js b/src/App.js index e40dc8a..b6e36ea 100644 --- a/src/App.js +++ b/src/App.js @@ -3,49 +3,51 @@ import { Console } from "@woowacourse/mission-utils"; class App { async run() { const sumResult = async (input) => { - - input = input.replace(/\\n/g, "\n"); + input = input.replace(/\\n/g, "\n"); - let delimiter = /[,:]/; - const escapeRegExp = (str) => - str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); + let delimiter = /[,:]/; + const escapeRegExp = (str) => + str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); - if (input.startsWith("//")) { - const delimiterEndIndex = input.search(/\n/); + if (input.startsWith("//")) { + const delimiterEndIndex = input.search(/\n/); - if (delimiterEndIndex === -1) { - throw new Error("\\n이 필요합니다."); - } - - let customDelimiter = input.slice(2, delimiterEndIndex); - if (/(.)\1+/.test(customDelimiter)) { - throw new Error("중복된 구분자를 사용할 수 없습니다."); - } - if (customDelimiter === ".") { - throw new Error(".은 구분자로 사용할 수 없습니다."); - } - input = input.slice(delimiterEndIndex + 1); + if (delimiterEndIndex === -1) { + throw new Error("\\n이 필요합니다."); + } - delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); - } else if (!(input.startsWith("//") || !isNaN(Number(input[0])))) { - throw new Error("양수 또는 //로 시작해야 합니다."); + let customDelimiter = input.slice(2, delimiterEndIndex); + if (/(.)\1+/.test(customDelimiter)) { + throw new Error("중복된 구분자를 사용할 수 없습니다."); } + // if (customDelimiter === ".") { + // throw new Error(".은 구분자로 사용할 수 없습니다."); + // } + input = input.slice(delimiterEndIndex + 1); + + delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); + } else if (!(input.startsWith("//") || !isNaN(Number(input[0])))) { + throw new Error("양수 또는 //로 시작해야 합니다."); + } - const inputArr = input.split(delimiter); + const inputArr = input.split(delimiter); - const sum = inputArr.reduce((acc, cur) => { - const num = Number(cur); - if (!isNaN(num)) { - if (num < 0) { - throw new Error("음수를 입력할 수 없습니다."); - } - return acc + num; + const sum = inputArr.reduce((acc, cur) => { + const num = Number(cur); + if (!isNaN(num)) { + if (num < 0) { + throw new Error("음수를 입력할 수 없습니다."); } + else if (!Number.isInteger(num)){ + throw new Error("정수만 입력 가능합니다.") + } + return acc + num; + } else { throw new Error("유효하지 않은 문자가 포함되어있습니다."); - }, 0); + } + }, 0); - return sum; - + return sum.toFixed(1); }; try {