-
Notifications
You must be signed in to change notification settings - Fork 3
[문자열 계산기] 박현규 미션 제출합니다. #4
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
Open
ParkHyeonkyu
wants to merge
7
commits into
Java-JavaScript-Language-Stuty:main
Choose a base branch
from
ParkHyeonkyu:ParkHyeonkyu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0994106
feat: 문자열 입력 요구기능 구현
ParkHyeonkyu 7336ea7
feat: 기본 구분자 덧셈 구현
ParkHyeonkyu 1529d7e
feat: 커스텀 구분자 구현
ParkHyeonkyu 3bd762c
feat:예외처리 구현
ParkHyeonkyu 0ceb087
style: Console 주석처리
ParkHyeonkyu ed245d4
docs: 주석제거
ParkHyeonkyu 0d42427
refactor: 피드백 반영 리팩토링
ParkHyeonkyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,19 @@ | ||
| # javascript-calculator-precourse | ||
| # javascript-calculator-precourse | ||
|
|
||
| ## 기능 | ||
|
|
||
| 1. 문자열 입력을 요구하는 기능 | ||
| 2. 유효한(기본, 커스텀) 구분자를 기준으로 숫자를 추출하고 더하는 기능 | ||
| 쉼표(,)와 콜론(:)은 기본 구분자로, //와 \n 사이에 위치하는 문자는 커스텀 구분자로 지정 | ||
| 2-1) 기본 구분자 구현 | ||
| 2-2) 커스텀 구분자 적용 구현 | ||
| 3. 예외 처리 | ||
|
|
||
| ## 예외처리 | ||
| 1. 숫자와 유효 구분자 이외의 문자(한글, 영어) | ||
| 2. 잘못된 커스텀 구분자 설정 형식 | ||
| 2-1) //로 시작해서 \n로 끝나지 않는 경우 | ||
| 2-2) 중복된 구분자를 사용하는 경우 ex) //**\n | ||
| 3. 유효하지 않은 구분자 사용 ex) //*\n1,2#3 | ||
| 4. 음수 사용 ex) -1,-2,3 | ||
| 5. 소수점을 커스텀 구분자로 사용하려는 경우 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,4 +40,4 @@ describe("문자열 계산기", () => { | |
|
|
||
| await expect(app.run()).rejects.toThrow("[ERROR]"); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,65 @@ | ||
| import { Console } from "@woowacourse/mission-utils"; | ||
|
|
||
| class App { | ||
| async run() {} | ||
| async run() { | ||
| const sumResult = async (input) => { | ||
| input = input.replace(/\\n/g, "\n"); | ||
|
|
||
| let delimiter = /[,:]/; | ||
| const escapeRegExp = (str) => | ||
| str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); | ||
|
|
||
| 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); | ||
|
|
||
| delimiter = new RegExp(`[,:${escapeRegExp(customDelimiter)}]`); | ||
| } else if (!(input.startsWith("//") || !isNaN(Number(input[0])))) { | ||
| throw new Error("양수 또는 //로 시작해야 합니다."); | ||
| } | ||
|
|
||
| const inputArr = input.split(delimiter); | ||
|
|
||
| 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); | ||
|
|
||
| return sum.toFixed(1); | ||
| }; | ||
|
|
||
| try { | ||
| const userInput = await Console.readLineAsync( | ||
| `덧셈할 문자열을 입력해주세요.\n` | ||
| ); | ||
| const output = await sumResult(userInput); | ||
| Console.print("결과 : " + output); | ||
| } catch (error) { | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default App; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import App from "./App.js"; | ||
|
|
||
| const app = new App(); | ||
| await app.run(); | ||
| await app.run(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
길이가 길어서 가독성이 떨어진다면 검증 과정의 책임을 분리해도 좋을 것 같습니다.