Conversation
KDG/Day15/계산기.html
Outdated
| const A4 = () => getElement("in-text"); | ||
|
|
||
| const print = (ingcle) => { | ||
| ingcle === "Error" ? alert("0으로 나눌 수 없습니다") : | ||
| A4().value = Number(ingcle).toFixed(2); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
There was a problem hiding this comment.
실제로 계산이 끝난 값이 출력이 되고는 있습니다. 그러나 사용자가 수식을 입력한 input창 내부의 text를 변경하는 것이 아니라, input창 외부에 배치한 다른 text가 없는 빈 태그(span, p 등)에 값을 전달해서 표현해주는 방식이 과제 풀이 방식입니다.
크게 어렵지 않습니다만, 다른 요소의 value를 바꾸는 식으로 작성해보는 것을 도전해보셔도 좋을것 같아요.
KDG/Day15/계산기.html
Outdated
| <script> | ||
| function getElement(id){ | ||
| return document.getElementById(id); | ||
| } | ||
| const inbtn = getElement("in-btn"); | ||
| const intext = () => getElement("in-text").value; | ||
|
|
||
|
|
||
| const click = () => { | ||
| const userInput = intext(); | ||
| const result = calculate(userInput); | ||
| print(result); | ||
| } | ||
| const exit = () => alert("계산기를 종료합니다."); | ||
|
|
||
| const exitcheck = (exit) => (exit === "exit" ) ? true : false; | ||
| function calculate(userInput) { | ||
| let result; | ||
| let n1,n2; | ||
| [n1, n2] = userInput.split(/[\+\-\*\/]/).map(Number); | ||
| switch (true){ | ||
| case userInput.includes("+"): | ||
| result = add(n1, n2); | ||
| break; | ||
| case userInput.includes("-"): | ||
| result = minus(n1, n2); | ||
| break; | ||
| case userInput.includes("*"): | ||
| result = multipl(n1, n2); | ||
| break; | ||
| case userInput.includes("/"): | ||
| result = division(n1, n2); | ||
| break; | ||
| case exitcheck(userInput): | ||
| exit(); | ||
| break; | ||
|
|
||
| } | ||
| return result; | ||
|
|
||
| } | ||
| inbtn.onclick = click; | ||
|
|
||
| const add = (n1, n2) => n1 + n2; | ||
| const minus = (n1, n2) => n1 - n2; | ||
| const multipl = (n1, n2) => n1 * n2; | ||
| const division = (n1, n2) => n2 === 0 ? "Error" : n1 / n2; | ||
| const A4 = () => getElement("in-text"); | ||
|
|
||
| const print = (ingcle) => { | ||
| ingcle === "Error" ? alert("0으로 나눌 수 없습니다") : | ||
| A4().value = Number(ingcle).toFixed(2); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| </script> |
There was a problem hiding this comment.
script를 html 파일과 분리하여 작성해주시면 좋을 것 같아요~
KDG/Day15/계산기.html
Outdated
| <input type = "text" id = "in-text" /> | ||
| <input type="button" id="in-btn" value="버튼" /> |
There was a problem hiding this comment.
| <input type = "text" id = "in-text" /> | |
| <input type="button" id="in-btn" value="버튼" /> | |
| <input type="text" id="in-text" /> | |
| <input type="button" id="in-btn" value="버튼" /> |
띄어쓰기 및 공백은 일관되게 작성해주세요!
KDG/Day15/계산기.html
Outdated
| function getElement(id){ | ||
| return document.getElementById(id); | ||
| } | ||
| const inbtn = getElement("in-btn"); | ||
| const intext = () => getElement("in-text").value; |
There was a problem hiding this comment.
| function getElement(id){ | |
| return document.getElementById(id); | |
| } | |
| const inbtn = getElement("in-btn"); | |
| const intext = () => getElement("in-text").value; | |
| const inbtn = document.getElementById("in-btn"); | |
| const intext = document.getElementById("in-text").value; |
처럼 작성해봐도 좋지 않을까요? 이미 만들어진 메서드(getElementById())를 함수로 한번 더 감싸서 사용할 필요는 없는것 같아요.
KDG/Day15/계산기.html
Outdated
| function calculate(userInput) { | ||
| let result; | ||
| let n1,n2; | ||
| [n1, n2] = userInput.split(/[\+\-\*\/]/).map(Number); | ||
| switch (true){ | ||
| case userInput.includes("+"): | ||
| result = add(n1, n2); | ||
| break; | ||
| case userInput.includes("-"): | ||
| result = minus(n1, n2); | ||
| break; | ||
| case userInput.includes("*"): | ||
| result = multipl(n1, n2); | ||
| break; | ||
| case userInput.includes("/"): | ||
| result = division(n1, n2); | ||
| break; | ||
| case exitcheck(userInput): | ||
| exit(); | ||
| break; | ||
|
|
||
| } | ||
| return result; | ||
|
|
||
| } |
There was a problem hiding this comment.
2+2/4 등의 수식 결과가 왜 4.00으로 출력될까요? 한번 소스코드를 보며 고민해봅시다.
electrohyun
left a comment
There was a problem hiding this comment.
말씀드린 부분에 대해서 피드백을 적용하여 코드를 재작성해주시거나, 제 코멘트에 대한 생각을 코멘트로 남겨주세요. 처음 커밋 코드보다 더 나아지신 것 같아서 보기 좋습니다. 고생하셨어요!
| <input type = "text" id = "in-text" /> | ||
| <input type = "button" id="in-btn" value="버튼" /> | ||
| <span id = "result-display"></span> |
There was a problem hiding this comment.
in-text, in-btn, result-display 등 케밥 케이스로 요소들의 id를 작성해주셨는데요, js 파일을 작성하시면서 문득 getElementById라는 메서드를 보신 적 있을거에요.
저희는 주로 로워 카멜 케이스(Lower Camel Case)로 변수명과 함수명을 작성하고 있는데요, 거기에 맞춰 따른다면, inText, inBtn, resultDisplay 등의 이름을 지어줄 수 있겠습니다.
타 11기 인원분들 진행하실때에도 같은 코멘트를 남겼었는데, 해당 내용 간단히 살펴보시면 좋을 것 같아요.
| const intext1 = () =>document.getElementById("in-text").value; | ||
| const intext = intext1; |
There was a problem hiding this comment.
| const intext1 = () =>document.getElementById("in-text").value; | |
| const intext = intext1; | |
| const inText = () =>document.getElementById("in-text").value; |
두개가 있을 필요는 없을것 같습니다.😅
추가적으로 위 코멘트에서 남긴 로워 카멜 케이스 적용해보세용
| }} | ||
| const exit = () => alert("계산기를 종료합니다."); | ||
|
|
||
| const exitcheck = (exit) => (exit === "exit" ) ? true : false; |
There was a problem hiding this comment.
| const exitcheck = (exit) => (exit === "exit" ) ? true : false; | |
| const isExit = (value) => value === "exit"; |
function (a, b) {
return a + b;
}
같은 구조가 있을때, 함수가 특정 작업을 하기 위해 부여받는 매개변수를 인자라고 합니다(a, b)
여기서는 매개변수의 역할을 exit라는 변수가 하고 있는데요, 변수 그 자체의 의미가 exit가 아닌지라, input(입력값)이나 value(값) 정도의 이름을 주면 코드를 읽기 수월할 것 같아요.
추가적으로, 해당 값을 "exit"와 같냐고 비교하는 것 자체만으로, true 혹은 false를 반환하기 때문에 삼항 연산자를 쓰지 않더라도 괜찮습니다. 😊
(이미 잘 하셨지만, 변수명에 대해서는, true 혹은 false를 반환하는 만큼, isExit 라는 이름이 가장 좋다고 생각해요.)
No description provided.