-
Notifications
You must be signed in to change notification settings - Fork 234
[문자열 덧셈 계산기] 박진성 미션 제출합니다 #231
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
base: main
Are you sure you want to change the base?
Changes from all commits
cdea12b
220c178
d2d4572
2f5f5b1
10e5347
e707488
d1e08e8
5091428
840ff28
0a98437
dec68ca
34d4cd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,49 @@ | ||
| # javascript-calculator-precourse | ||
| # 1주차 프리코스 과제 : 문자열 덧셈 계산기 | ||
| - 입력한 문자열에서 숫자를 추출하여 더하는 계산기 | ||
|
|
||
| ## 기본 기능 | ||
| - 구분자와 입력한 숫자들 중 숫자들을 더하여 출력 | ||
| - 문자열 중 숫자가 없을 시 0을 반환 | ||
|
|
||
| ## 입출력 요구사항 | ||
| - 입력은 구분자와 양수로 구성된 문자열 | ||
| - 출력의 형식은 덧셈 | ||
|
|
||
| ## 기본 구문자 | ||
| - 쉼표(,) | ||
| - 콜론(:) | ||
| ### 기본 구문자 예 | ||
| - "" => 0 | ||
| - "1,2" => 3 | ||
| - "2:3" => 5 | ||
| - "3,4:5" => 12 | ||
|
|
||
| ## 커스텀 구문자 | ||
| - 문자열 앞부분 "//"와 "\n"사이에 위치하는 문자 | ||
| ### 커스텀 구문자 예 | ||
| - //;\n1;2;3 => 6 | ||
|
|
||
| ## 예외 처리 | ||
| - 사용자가 기본 구문자와 커스텀 구문자 외 잘못된 입력값이 입력될 경우 "[ERROR]"의 메시지와 함께 error를 발생시키고 애플리케이션 종료 | ||
|
|
||
| ## 실행 예시 | ||
|
|
||
| --------------------------- | ||
| 덧셈할 문자열을 입력해 주세요. | ||
| 1,2:3 | ||
| --------------------------- | ||
| 결과 : 6 | ||
| --------------------------- | ||
|
|
||
| ## 구현 목록 | ||
| - [ ] 값 입력 | ||
| - [ ] 구분자 설정(기본 구분자,커스텀 구분자) | ||
| - [ ] 숫자 추출 | ||
| - [ ] 덧셈 계산 | ||
| - [ ] 예외 처리 | ||
| - [ ] 계산 결과 출력 | ||
|
|
||
| ## 소감문 | ||
| 이번 주 과제를 통해 Node.js를 처음으로 코드를 만들어 본 경험을 해본 것 같습니다. | ||
| 문자열 덧셈 계산기를 통해 기본적인 js 문법과 메서드를 알 수 있게된 좋은 경험을 가지고 온 것 같고 최대한 메서드를 이용하여 클린한 코드를 만들어보려고 하였지만 | ||
| 아직은 조금 부족한 것 같아 다음주 과제를 통해 조금 더 나은 코드 구조를 적용해보고, 리뷰 과정을 통해 배운 것을 개선해 나가도록 노력하겠습니다. |
| 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() { | ||||||||||||||||||||||||||||||||||||
| try{ | ||||||||||||||||||||||||||||||||||||
| //1.값 입력 | ||||||||||||||||||||||||||||||||||||
| Console.print("덧셈할 문자열을 작성하세요."); | ||||||||||||||||||||||||||||||||||||
| const input = await Console.readLineAsync(""); | ||||||||||||||||||||||||||||||||||||
| const result = this.calculate(input); | ||||||||||||||||||||||||||||||||||||
| Console.print(`결과 : ${result}`); | ||||||||||||||||||||||||||||||||||||
| }catch(error){ | ||||||||||||||||||||||||||||||||||||
| //예외 발생시 에러 메시지 출력 | ||||||||||||||||||||||||||||||||||||
| Console.print(error.message); | ||||||||||||||||||||||||||||||||||||
| // 에러 메시지 출력 후, 에러를 알리기 위해 에러를 다시 던짐 | ||||||||||||||||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| calculate(input){ | ||||||||||||||||||||||||||||||||||||
| //3.숫자 추출 | ||||||||||||||||||||||||||||||||||||
| input = input.trim(); | ||||||||||||||||||||||||||||||||||||
| //3.1 input의 값이 없거나 null이라면 0을 반환 | ||||||||||||||||||||||||||||||||||||
| if(!input || input.trim().length === 0){ | ||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| //2.구분자 설정 | ||||||||||||||||||||||||||||||||||||
| let numberString = input; | ||||||||||||||||||||||||||||||||||||
| let defaultDelimiters = [',',':']; | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||||||
| const customDelimiters = input.match(/\/\/(.)\\n(.*)/); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| //customDelimiters에 match의 형식이 일치할 때(형식이 일치하지 않으면 null 값을 반환하여 진행하지 않음) | ||||||||||||||||||||||||||||||||||||
| if(customDelimiters){ | ||||||||||||||||||||||||||||||||||||
| const customChar = customDelimiters[1]; | ||||||||||||||||||||||||||||||||||||
| numberString = customDelimiters[2]; | ||||||||||||||||||||||||||||||||||||
| defaultDelimiters.push(customChar); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| //3.2 defaultDelimiters의 구분자에 따라 split을 사용하여 값을 쪼갠다. | ||||||||||||||||||||||||||||||||||||
| const delimitersRegExp = new RegExp(`[${defaultDelimiters.join('')}]`); | ||||||||||||||||||||||||||||||||||||
| const numberExtraction = numberString.split(delimitersRegExp); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| export default App; | ||||||||||||||||||||||||||||||||||||
| //4.덧셈 계산 | ||||||||||||||||||||||||||||||||||||
| const numberList = numberExtraction.map(Number); | ||||||||||||||||||||||||||||||||||||
| let sum = 0; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| //5.예외처리 | ||||||||||||||||||||||||||||||||||||
| for(let i = 0;i < numberList.length;i++){ | ||||||||||||||||||||||||||||||||||||
| //numberList에 숫자가 아닌 다른 값이 들어왔을 때 | ||||||||||||||||||||||||||||||||||||
| if(isNaN(numberList[i])){ | ||||||||||||||||||||||||||||||||||||
| throw new Error("[ERROR] 숫자로 변환할 수 없는 문자가 포함되어 있습니다."); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| //numberList에 음수의 숫자 값이 들어왔을 때 | ||||||||||||||||||||||||||||||||||||
| if(numberList[i] < 0){ | ||||||||||||||||||||||||||||||||||||
| throw new Error("[ERROR] 음수로 입력할 수 없습니다."); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 들여쓰기가 깊이가 깊어져서 한 번에 하나의 검증을 적용하면 어떨까요?
Suggested change
이전에 number 형으로 변환해서 |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| //4.1 리스트에 있는 값들을 더함 | ||||||||||||||||||||||||||||||||||||
| for(let i = 0; i < numberList.length;i++){ | ||||||||||||||||||||||||||||||||||||
| sum += numberList[i]; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. airbnb 린트를 따르면 for 문은 오류나더라고요.. |
||||||||||||||||||||||||||||||||||||
| return sum; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| export default App; | ||||||||||||||||||||||||||||||||||||
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.
여기 인자로 "덧셈할 문자열을 작성하세요." 넣으면 위에 코드 생략할 수 있어요!