Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[문자열 덧셈 계산기] 권혁준 미션 제출합니다. #592

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# javascript-calculator-precourse
# javascript-calculator-precourse

## 구현 기능 목록

### ✅ 기능 요구 사항

- [ ] 입출력
- `Console` API에서 제공하는 `Console.readLineAsync()` 메서드를 활용하여 문자열을 입력한다.
- `Console.print()` 메서드를 활용하여 결과를 출력한다.
- [ ] 기본 구분자를 이용한 계산
- `,` 또는 `:` 을 구분자로 가지는 문자열을 전달할 경우, 구분자를 기준으로 분리한 각 숫자의 합을 반환한다.
- 예: `"1,2"` => `3` / `"1,2:3"` => `6`
- 빈 문자열의 경우, 0을 반환한다.
- [ ] 커스텀 구분자를 이용한 계산
- 문자열 앞부분의 `//`와 `\n` 사이에 위치하는 문자를 커스텀 구분자로 사용한다.
- 예를 들어 `"//;\n1;2;3"`과 같이 값을 입력할 경우 커스텀 구분자는 `;`이며, 결과 값은 6이 반환되어야 한다.

---

### ✅ 유효성 검사

- [ ] 빈 문자열을 입력하면 0를 반환한다.
- [ ] 문자열 내 음수가 포함되면, 에러 메세지와 함께 에러를 발생시킨다.
- [ ] 입력 문자 내 숫자가 아닌 문자 포함 시, 에러 메세지와 함께 에러를 발생시킨다.
- [ ] 커스텀 구분자 사용 시, 유효하지 않는 구분자면 에러 메세지와 함께 에러를 발생시킨다.
76 changes: 75 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,79 @@
import { Console } from '@woowacourse/mission-utils';

class App {
async run() {}
async run() {
const INPUT_PROMPT = '덧셈할 문자열을 입력해 주세요: ';
const userInputString = await Console.readLineAsync(INPUT_PROMPT);

try {
const sumResult = this.calculateSum(userInputString);
Console.print(`결과 : ${sumResult}`);
} catch (error) {
Console.print(error.message);
throw error;
}
}

// 입력 받은 문자열 안의 숫자 합계 계산 (input: string, output: number)
calculateSum(inputString) {
// 입력이 비어있으면 0을 반환
if (inputString.trim() === '') {
return 0;
}

const { numbersString, customSeparator } = this.parseInput(inputString);
const numberArray = this.extractNumbers(numbersString, customSeparator);
this.validateNumbers(numberArray);

return numberArray.reduce((total, num) => total + num, 0);
}

// 입력 문자열을 파싱하여 숫자 부분과 커스텀 구분자를 추출
parseInput(inputString) {
const customSeparatorPattern = /^\/\/(.)\\n/;
const matchResult = inputString.match(customSeparatorPattern);
if (matchResult) {
return {
numbersString: inputString.replace(/^\/\/(.)\\n/, '').trim(),
customSeparator: matchResult[1],
};
}
return { numbersString: inputString, customSeparator: null };
}

// 기본 구분자 쉼표(,)와 콜론(:)를 기준으로 입력받은 문자열 분리 후 숫자로 변환 (input: string, output: number[])
extractNumbers(numbersString, customSeparator) {
let separators = [',', ':'];
if (customSeparator) {
separators = [customSeparator];
}
const separatorRegex = new RegExp(
`[${separators
.map((sep) => sep.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'))
.join('')}]`,
'g'
);
const separatedInput = numbersString.replace(separatorRegex, ',');
const splitValues = separatedInput.split(',').map((num) => num.trim());

if (splitValues.some((value) => value === '')) {
throw new Error('[ERROR] 구분자 사이에 숫자가 없습니다');
}

return splitValues.map(Number);
}

validateNumbers(numberArray) {
// 숫자가 아닌 값이 있으면 오류 발생
if (numberArray.some((num) => isNaN(num) || !Number.isInteger(num))) {
throw new Error('[ERROR] 잘못된 값이 입력되었습니다');
}

// 0이나 음수가 포함되어 있으면 오류 발생
if (numberArray.some((num) => num <= 0)) {
throw new Error('[ERROR] 입력값은 양수로만 구성되어야 합니다');
}
}
}

export default App;