-
Notifications
You must be signed in to change notification settings - Fork 232
[문자열 덧셈 계산기] 강하영 미션 제출합니다. #227
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
Hayoung0708
wants to merge
16
commits into
woowacourse-precourse:main
Choose a base branch
from
Hayoung0708:hayoung0708
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
16 commits
Select commit
Hold shift + click to select a range
f32075e
docs(readme): 요구 사항 및 구현할 기능 작성
Hayoung0708 f9efa0c
feat(app): 문자열 입력 기능 구현
Hayoung0708 b43149b
feat(app): 기본 구분자 기준 숫자 분리 기능 구현
Hayoung0708 8b405c1
feat(app): 계산 및 결과 반환 기능 구현
Hayoung0708 8edb83c
feat(app): 커스텀 구분자 추가 기능 구현
Hayoung0708 5f80ed4
feat(app): 빈 문자열을 입력한 경우 예외처리
Hayoung0708 0f2a5fd
docs(readme): 기능 및 예외 수정
Hayoung0708 e0b8265
fix(app): 커스텀 구분자 추가 기능 수정
Hayoung0708 92e55f4
feat(app): 지정된 구분자 외에 다른 문자가 포함된 경우 예외처리
Hayoung0708 7209c7f
docs(readme): 예외 수정
Hayoung0708 33fa7a3
feat(app): 커스텀 구분자 지정이 잘못된 경우 예외처리
Hayoung0708 c9a55ef
feat(app): 숫자에 0을 입력한 경우 예외처리
Hayoung0708 4d8e1b5
refactor(app): 코드 정리 및 반복문 개선
Hayoung0708 6a1908d
docs(readme): 최종 정리
Hayoung0708 da95c91
fix(app): 정규식에서 숫자가 포함되지 않는 오류 수정
Hayoung0708 a63e883
Merge branch 'hayoung0708' of https://github.com/Hayoung0708/javascri…
Hayoung0708 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,64 @@ | ||
| # javascript-calculator-precourse | ||
| # 문자열 덧셈 계산기 | ||
|
|
||
| ## 기능 요구 사항 | ||
|
|
||
| 입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다. | ||
|
|
||
| - 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다. | ||
| - 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6 | ||
| - 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다. | ||
| - 예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. | ||
| - 사용자가 잘못된 값을 입력할 경우 "[ERROR]"로 시작하는 메시지와 함께 Error를 발생시킨 후 애플리케이션은 종료되어야 한다. | ||
|
|
||
| ## 입출력 요구 사항 | ||
|
|
||
| #### 입력 | ||
|
|
||
| - 구분자와 양수로 구성된 문자열 | ||
|
|
||
| #### 출력 | ||
|
|
||
| - 덧셈 결과 | ||
|
|
||
| ``` | ||
| 결과 : 6 | ||
| ``` | ||
|
|
||
| #### 실행 결과 예시 | ||
|
|
||
| ``` | ||
| 덧셈할 문자열을 입력해 주세요. | ||
| 1,2:3 | ||
| 결과 : 6 | ||
| ``` | ||
|
|
||
| ## 프로그래밍 요구 사항 | ||
|
|
||
| - Node.js 22.19.0 버전에서 실행 가능해야 한다. | ||
| - 프로그램 실행의 시작점은 `App.js`의 `run()`이다. | ||
| - `package.json` 파일은 변경할 수 없으며, 제공된 라이브러리와 스타일 라이브러리 이외의 외부 라이브러리는 사용하지 않는다. | ||
| - 프로그램 종료 시 `process.exit()`를 호출하지 않는다. | ||
| - 프로그래밍 요구 사항에서 달리 명시하지 않는 한 파일, 패키지 등의 이름을 바꾸거나 이동하지 않는다. | ||
| - 자바스크립트 코드 컨벤션을 지키면서 프로그래밍한다. | ||
| - 기본적으로 [JavaScript Style Guide](https://github.com/woowacourse/woowacourse-docs/tree/main/styleguide/javascript)를 원칙으로 한다. | ||
|
|
||
| ### 라이브러리 | ||
|
|
||
| - `@woowacourse/mission-utils`에서 제공하는 `Console` API를 사용하여 구현해야 한다. | ||
| - 사용자의 값을 입력 및 출력하려면 `Console.readLineAsync()`와 `Console.print()`를 활용한다. | ||
|
|
||
| ## 구현한 기능 | ||
|
|
||
| #### 1. 문자열 입력 | ||
| #### 2. 쉼표(,), 콜론(:) 구분자를 기준으로 숫자 분리 | ||
| #### 3. 계산 및 결과 반환 | ||
| #### 4. 커스텀 구분자 추가 | ||
| #### 5. 예외 처리 | ||
|
|
||
| - 빈 문자열을 입력한 경우 | ||
| - 지정된 구분자 외에 다른 문자가 포함된 경우 | ||
| - 커스텀 구분자 지정이 잘못된 경우 | ||
| - 맨 앞에 작성되지 않은 경우 | ||
| - 구분자가 없거나 두 개 이상 작성된 경우 | ||
| - 구분자에 (,)나 (:)가 작성된 경우 | ||
| - 숫자에 0을 입력한 경우 |
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,64 @@ | ||
| import { Console } from "@woowacourse/mission-utils"; | ||
|
|
||
| class App { | ||
| async run() {} | ||
| async run() { | ||
| /* 문자열 입력 */ | ||
| let input = await Console.readLineAsync( | ||
| "덧셈할 문자열을 입력해 주세요.\n" | ||
| ); | ||
|
|
||
| // 예외처리 - 빈 문자열을 입력한 경우 | ||
| if (input === "") { | ||
| return Console.print("결과 : 0"); | ||
| } | ||
|
|
||
| /* 커스텀 구분자 추가 */ | ||
| let custom = ""; | ||
| let customForm = input.match(/\/\/.*\\n/); | ||
|
|
||
| if (customForm) { | ||
| // 예외처리 - 커스텀 구분자 지정이 잘못된 경우 | ||
| if (customForm.index !== 0) { | ||
| throw new Error( | ||
| "[ERROR] 커스텀 구분자 지정 형식은 맨 앞에 작성해야 합니다." | ||
| ); | ||
| } | ||
| if (customForm[0].length !== 5) { | ||
| throw new Error( | ||
| "[ERROR] 커스텀 구분자는 한 개 작성해야 합니다." | ||
| ); | ||
| } | ||
| if (customForm[0][2] === "," || customForm[0][2] === ":") { | ||
| throw new Error( | ||
| "[ERROR] 기본 구분자는 커스텀 지정자에 작성할 수 없습니다." | ||
| ); | ||
| } | ||
|
|
||
| custom = `|${input[2]}`; | ||
| input = input.slice(5); | ||
| } | ||
|
|
||
| // 예외처리 - 지정된 구분자 외에 다른 문자가 포함된 경우 | ||
| let pattern = new RegExp(`^[0-9,|:${custom}]+$`); | ||
| if (!pattern.test(input)) { | ||
| throw new Error( | ||
| "[ERROR] 지정된 구분자 외에 다른 문자가 포함되어 있습니다." | ||
| ); | ||
| } | ||
|
|
||
| /* 숫자 분리 */ | ||
| let delimiter = new RegExp(`,|:${custom}`); | ||
| let number = input.split(delimiter); | ||
|
|
||
| // 예외처리 - 숫자에 0을 입력한 경우 | ||
| if (number.includes("0")) { | ||
| throw new Error("[ERROR] 양수만 입력할 수 있습니다."); | ||
| } | ||
|
|
||
| /* 계산 및 결과 반환 */ | ||
| let answer = number.reduce((sum, num) => (sum += +num), 0); | ||
| Console.print(`결과 : ${answer}`); | ||
| } | ||
| } | ||
|
|
||
| export default App; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
숫자 형변환을 하는 경우
Number(num)으로 강제해야된다고 하네요..!22.3 숫자: 형변환을 하는 경우 Number를 사용하고, 문자열을 파싱하는 경우에는 기수를 인자로 넘겨 parseInt를 사용하세요.