-
Notifications
You must be signed in to change notification settings - Fork 37
Step2-2 #94
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
Step2-2 #94
Changes from all commits
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,42 +1,76 @@ | ||
| const PIE = Math.PI; | ||
| let areaSum = 0; | ||
| let stackList = []; | ||
|
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를 써도 되겠죠? |
||
|
|
||
| module.exports.circle = function(r = -1){ | ||
| function circle(r = -1){ | ||
|
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. 그냥 circle보다는 areaCircle 같이 조금 더 명확한 이름이 좋습니다. 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. 다시 읽어보니 모듈의 이름이 area라서 circle도 괜찮을 것 같아요 😉 |
||
| checkParameter(r); | ||
| stackList.push("circle"); | ||
| return Math.pow(r, 2) * PIE; | ||
| } | ||
|
|
||
| module.exports.square = function(a = -1, b = -1){ | ||
| function circleSum(r = -1){ | ||
| checkParameter(r); | ||
|
|
||
| if(r > 0){ | ||
| areaSum += Math.pow(r, 2) * PIE; | ||
|
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. 이미 있는 함수를 재활용하는게 좋지 않을까요? |
||
| circleSum(r-1); | ||
| } else{ | ||
| stackList.push("circle"); | ||
| } | ||
|
|
||
| return areaSum; | ||
| } | ||
|
|
||
| function rect(a = -1, b = -1){ | ||
| checkParameter(a, b); | ||
| stackList.push("rect"); | ||
| return a * b; | ||
| } | ||
|
|
||
| module.exports.trapezoid = function(a = -1, b = -1, h = -1){ | ||
| function trapezoid(a = -1, b = -1, h = -1){ | ||
| checkParameter(a, b, h); | ||
| stackList.push("trapezoid"); | ||
| return (a + b) * h / 2; | ||
| } | ||
|
|
||
| module.exports.cylindrical = function(r = -1, h = -1){ | ||
| checkParameter(r, h); | ||
| return (this.circle(r) * 2) + (r * 2 * PIE * h); | ||
| } | ||
|
|
||
| function checkParamCount(rest){ | ||
| for (let i=0; i<rest.length; i++){ | ||
| if(rest[i] === -1){ | ||
| function checkParamCount(param){ | ||
| for (let i=0; i<param.length; i++){ | ||
| if(param[i] === -1){ | ||
| throw Error("모든 인자를 입력하세요."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function checkParamType(rest){ | ||
| for (let i=0; i<rest.length; i++) { | ||
| if(typeof rest[i] !== "number") { | ||
| function checkParamType(param){ | ||
| for (let i=0; i<param.length; i++) { | ||
| if(typeof param[i] !== "number") { | ||
| throw Error("숫자를 입력하세요."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function checkParameter(...rest){ | ||
| checkParamCount(rest); | ||
| checkParamType(rest); | ||
| function checkParameter(...param){ | ||
| checkParamCount(param); | ||
| checkParamType(param); | ||
| } | ||
|
|
||
| module.exports.printExecutionSequence = function(){ | ||
| let result = stackList.join(', '); | ||
| return result; | ||
| } | ||
|
|
||
| module.exports.getArea = function (...param){ | ||
| if (param[0] === 'circle'){ | ||
| if(param.length >= 3){ | ||
| return circleSum(param[2]); | ||
| }else{ | ||
| return circle(param[1]); | ||
| } | ||
| } else if(param[0] === 'rect'){ | ||
| return rect(param[1], param[2]); | ||
| } else if(param[0] === 'trapezoid'){ | ||
| return trapezoid(param[1], param[2], param[3]); | ||
| }else{ | ||
| throw Error("함수를 다시입력하세요."); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,7 @@ | ||
| const area = require("./area"); | ||
|
|
||
| console.log(area.circle(3)); | ||
| console.log(area.square(5, 6)); | ||
| console.log(area.trapezoid(5, 3, 2)); | ||
| console.log(area.cylindrical(2, 5)); | ||
|
|
||
| // console.log(area.circle('3')); | ||
| // console.log(area.square('아', 5)); | ||
| // console.log(area.trapezoid(5, 3, '3')); | ||
| // console.log(area.cylindrical(2)); | ||
| console.log(area.getArea('circle', 10)); | ||
| console.log(area.getArea('circle', 1, 4)); | ||
| console.log(area.getArea('rect', 10, 15)); | ||
| console.log(area.getArea('trapezoid', 10, 15, 12)); | ||
| console.log(area.printExecutionSequence()); |
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.
변수명은 PI 가 좋을 듯?