-
Notifications
You must be signed in to change notification settings - Fork 232
[문자열 덧셈 계산기] 이혁 미션 제출합니다. #234
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
29125ac
236c201
fbd1eeb
eb3c9cf
2c30489
b1b95a5
ed149af
3337c48
21b6f02
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,31 @@ | ||
| # javascript-calculator-precourse | ||
| # 1주차 미션: 문자열 덧셈 계산기 | ||
|
|
||
| ### 기능 요구 사항 | ||
|
|
||
| --- | ||
|
|
||
| 입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다. | ||
|
|
||
| - 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다. | ||
|
|
||
| - 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6 | ||
|
|
||
| - 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다. | ||
|
|
||
| - 예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. | ||
|
|
||
| - 사용자가 잘못된 값을 입력할 경우 "[ERROR]"로 시작하는 메시지와 함께 `Error`를 발생시킨 후 애플리케이션은 종료되어야 한다. | ||
|
|
||
| --- | ||
|
|
||
| ### 기능 구현 목록 | ||
|
|
||
| --- | ||
|
|
||
| - [ ] 입력 후 정규 표현식을 통해 커스텀 구분자를 확인하는 로직 | ||
| - [ ] 구분자와 숫자를 구분하여 합산 구하는 로직 | ||
|
|
||
| - [ ] 입력 전후 요구 사항에 맞게 결과 출력하는 로직 | ||
| - [ ] 에러 발생 시 에러 메시지 출력하는 로직 | ||
|
|
||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| import { Console } from "@woowacourse/mission-utils"; | ||
| import { | ||
| getCustomSeparator, | ||
| getExpression, | ||
| executeExpression, | ||
| } from "./function.js"; | ||
|
|
||
| class App { | ||
| async run() {} | ||
| async run() { | ||
| const input = await Console.readLineAsync("덧셈할 문자열을 입력해 주세요."); | ||
|
|
||
| const separators = getCustomSeparator(input); | ||
| const expressions = getExpression(input); | ||
| const result = executeExpression(expressions, separators); | ||
|
|
||
| Console.print(`결과 : ${result}`); | ||
| } | ||
| } | ||
|
|
||
| export default App; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| const pattern = /\/\/(.*)\\n/; | ||
|
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. [제안]
Author
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. 앗 그러네요! 다음부터는 저런 상수는 표기법 바꿔서 해야겠네요. 이름만으로 파악하기 어려워서 |
||
|
|
||
| function hasCustomSeparator(input) { | ||
| // 커스텀 구분자를 선언하는 데 이용되는 패턴 선언 | ||
| return pattern.test(input); | ||
| } | ||
|
|
||
| export function getCustomSeparator(input) { | ||
| let separators = [":", ","]; | ||
| if (hasCustomSeparator(input)) { | ||
| const customSeparators = input.match(pattern)[1]; | ||
| isLegelSeparator(customSeparators); | ||
|
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.
Author
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. 앗 오타가 있었네요 집어주셔서 감사합니다!! |
||
| for (const separator of customSeparators) { | ||
| separators.push(separator); | ||
| } | ||
| } | ||
| return separators; | ||
| } | ||
|
|
||
| export function getExpression(input) { | ||
| if (hasCustomSeparator(input)) { | ||
| return input.replace(pattern, ""); | ||
| } else { | ||
| return input; | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+26
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. [제안]
Author
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. 사실 저도 expression이라는 이름이 살짝 애매하다고 생각했는데, inputWithoutSeparatorPattern라고 쓰면 좀 길어지더라도 훨씬 명확해지네요! 이번 주차부터 JSDoc에 대해서 조금 공부해서 그거와 함께 주석을 추가하는 것도 좋은 방법일거 같습니다 |
||
|
|
||
| export function executeExpression(expressions, separators) { | ||
| let stack = ""; | ||
| let result = 0; | ||
|
|
||
| for (const char of expressions) { | ||
|
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. 문자열을 한 글자씩 순회하면서 직접 숫자를 만들어가는 구조가 흥미로웠어요! 평소엔 split()으로 나누는 방식만 생각했는데, 이런 식으로 스택처럼 직접 쌓았다가 처리하는 방법도 가능하다는 걸 배웠습니다. |
||
| if (separators.includes(char)) { | ||
| number = Number(stack); | ||
|
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.
Author
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. 엇 저부분 무슨 생각을 하고 작성했는지 기억이 안나네요 다음부터는 조금더 깊게 생각하고 작성해봐야 할거 같습니다! |
||
| isLegalNumber(number); | ||
| result += number; | ||
| stack = ""; | ||
| } else stack += char; | ||
| } | ||
|
|
||
| if (stack === "") throw new Error("[ERROR] 구분자로 마무리될 수 없어요."); | ||
|
|
||
| number = Number(stack); | ||
| isLegalNumber(number); | ||
| result += Number(stack); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function isLegalNumber(number) { | ||
| if (number < 0) throw new Error("[ERROR] 양수를 입력해주세요."); | ||
| if (Number.isNaN(number)) | ||
| throw new Error("[ERROR] 올바른 숫자를 입력해주세요."); | ||
| } | ||
|
|
||
| function isLegelSeparator(separators) { | ||
| if (separators.length < 1) | ||
| throw new Error("[ERROR] 구분자를 올바르게 입력해주세요."); | ||
| if (/.*\d.*/.test(separators)) | ||
| throw new Error("[ERROR] 숫자를 구분자로 이용할 수 없어요."); | ||
| } | ||
|
Comment on lines
+50
to
+61
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. [제안]
Author
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. 앗 저도 is라고 적은게 맞는 표현인지 몰랐는데 validate라는 좋은 단어가 있네요!! 2주차부터 바로 반영해보겠습니다!
Author
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. 좋은 제안 많이 해주셔서 감사합니다 😊 큰 도움되었습니다!! |
||
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.
모듈을 나누어 import하는 구조로 코드를 작성할 수 있군요!
저는 기능 구현에 집중하느라 가독성 있게 구조를 나누는 방법까지는 생각하지 못했는데, 이 코드를 보고 배웠습니다 :)