Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@

## 📝 License
This project is [MIT](https://github.com/woowacourse/javascript-calculator/blob/master/LICENSE) licensed.

## 구현할 기능 목록

1. 입력값 받기
2. 문자열에서 숫자 추출
3. 쉼표(,) 또는 콜론(:) 구분자 처리
4. 커스텀 구분자 처리 (예: //;\n1;2;3)
5. 잘못된 입력 시 [ERROR] 출력
6. 합계 계산 및 결과 출력
16 changes: 16 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Calculator from "./Calculator.js";

class App {
run() {
const input = prompt("덧셈할 문자열을 입력해 주세요."); // 사용자 입력
try {
const result = Calculator.add(input);
console.log(`결과 : ${result}`);
} catch (error) {
console.error("[ERROR]", error.message);
}
}
}

const app = new App();
app.run();
15 changes: 15 additions & 0 deletions src/Calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Parser from "./Parser.js";
import Validator from "./Validator.js";

class Calculator {
static add(input) {
if (!input) return 0;

const numbers = Parser.parse(input);
Validator.check(numbers);

return numbers.reduce((acc, cur) => acc + cur, 0);
}
}

export default Calculator;
15 changes: 15 additions & 0 deletions src/Parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Parser {
static parse(text) {
// 커스텀 구분자 패턴 확인: //;\n1;2;3
const customDelimiterMatch = text.match(/^\/\/(.)\\n(.*)$/);
if (customDelimiterMatch) {
const [, delimiter, numbers] = customDelimiterMatch;
return numbers.split(delimiter).map(Number);
}

// 기본 구분자(, :) 처리
return text.split(/,|:/).map(Number);
}
}

export default Parser;
14 changes: 14 additions & 0 deletions src/Validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Validator {
static check(numbers) {
numbers.forEach((num) => {
if (isNaN(num)) {
throw new Error("숫자가 아닌 값이 포함되어 있습니다.");
}
if (num < 0) {
throw new Error("음수는 입력할 수 없습니다.");
}
});
}
}

export default Validator;
36 changes: 36 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const DEFAULT_DELIMITERS = [",", ":"];

function calculate(input) {
if (!input) return 0;

const { delimiters, numbersString } = extractDelimiter(input);
const numbers = splitAndConvert(numbersString, delimiters);
validate(numbers);

return numbers.reduce((sum, num) => sum + num, 0);
}

function extractDelimiter(input) {
const customDelimiterPattern = /^\/\/(.)\n(.*)$/;
const match = input.match(customDelimiterPattern);

if (match) {
return { delimiters: [match[1]], numbersString: match[2] };
}

return { delimiters: DEFAULT_DELIMITERS, numbersString: input };
}

function splitAndConvert(str, delimiters) {
const regex = new RegExp(`[${delimiters.join("")}]`);
return str.split(regex).map(Number);
}

function validate(numbers) {
numbers.forEach((n) => {
if (isNaN(n)) throw new Error("[ERROR] 숫자가 아닌 값이 있습니다.");
if (n < 0) throw new Error("[ERROR] 음수는 입력할 수 없습니다.");
});
}

export default calculate;