-
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?
Conversation
라이브러리 import { Console } from "@woowacourse/mission-utils"; 생성 후, 안내 메시지를 Console.print로 출력하고, await Console.readLineAsync를 사용하여 사용자 입력을 비동기적 처리
run()과 calculate 메서드를 분리하여 기본 구분자 설정 및 커스텀 구분자 파싱
input 값에 따른 빈 문자열 처리와 구분자 설정에서 진행한 구분자 split
정수형 배열로 변환 후, for 루프를 사용하여 합 계산
숫자가 아닌 다른 값이 들어왔을 때와 음수의 값이 들어왔을 때의 예외처리
에러 메시지 출력 후, 에러를 알리기 위한 코드 작성
inseong01
left a comment
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.
커밋 바디에 작업 내용을 적어서 어떤 커밋인지 확인할 수 있어서 좋았어요. 헤더는 어떤 작업인지 핵심만 적으면 더 좋아질 거 같아요.
1주 차 미션하시느라 수고하셨습니다.
| try{ | ||
| //1.값 입력 | ||
| Console.print("덧셈할 문자열을 작성하세요."); | ||
| const input = await Console.readLineAsync(""); |
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.
여기 인자로 "덧셈할 문자열을 작성하세요." 넣으면 위에 코드 생략할 수 있어요!
| } | ||
| //2.구분자 설정 | ||
| let numberString = input; | ||
| let defaultDelimiters = [',',':']; |
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.
push는 재할당하는 게 아니어서 const로 선언해도 좋을 거 같아요.
| 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] 음수로 입력할 수 없습니다."); | ||
| } | ||
| } |
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.
들여쓰기가 깊이가 깊어져서 한 번에 하나의 검증을 적용하면 어떨까요?
| 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] 음수로 입력할 수 없습니다."); | |
| } | |
| } | |
| //numberList에 숫자가 아닌 다른 값이 들어왔을 때 | |
| const hasNaN = numberList.some(Number.isNaN); | |
| if (hasNaN) throw new Error("[ERROR] 숫자로 변환할 수 없는 문자가 포함되어 있습니다."); | |
| //numberList에 음수의 숫자 값이 들어왔을 때 | |
| const hasNegativeNum = numberList.some((num) => num < 0); | |
| if(hasNegativeNum) throw new Error("[ERROR] 음수로 입력할 수 없습니다."); |
이전에 number 형으로 변환해서 Number.isNaN()을 사용하면 isNaN 형변환 연산 중복을 줄일 수 있을 거 같아요.
| for(let i = 0; i < numberList.length;i++){ | ||
| sum += numberList[i]; | ||
| } |
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.
airbnb 린트를 따르면 for 문은 오류나더라고요..
그래서 reduce 매서드를 사용해서 합계를 반환하면 좋을 거 같아요.
No description provided.