-
Notifications
You must be signed in to change notification settings - Fork 37
step 2-2 feat: 출력함수구현, refactor: switch문 객체로 수정 #88
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const calc = require('./calc'); | ||
| // const calc = require('./calc-this'); | ||
|
|
||
|
|
||
| const {log} = console; | ||
|
|
||
| // // console.log(circleArea('4')) | ||
| // // console.log(rectancleArea(4,'5')); | ||
| // // rectancleArea('ㄱ','5','5'); | ||
| // // console.log(rectancleArea('5')); | ||
| // // console.log(cylinderArea('4',6)) | ||
|
|
||
| // log(calc.getCircleArea(4,6)); | ||
|
|
||
| log(calc.getArea('circle',10)); | ||
| log(calc.getArea('circle',1,3)); | ||
| log(calc.getArea('rect',10,14)); | ||
| log(calc.getArea('trapezoid',10,14,5)); | ||
|
|
||
| calc.printExecutionSequence(); | ||
| // log(calc.printExecutionSequence()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| const check = require('./check'); | ||
|
|
||
| function circleArea(param) { | ||
| const [r] = check.checkParam(1, param); | ||
| return Math.PI * r * r | ||
| } | ||
|
|
||
| function rectangleArea(param) { | ||
| const [w, h] = check.checkParam(2,param); | ||
| return w * h; | ||
| } | ||
|
|
||
| function trapezoidArea(param) { | ||
| const [t, b, h] = check.checkParam(3,param); | ||
| return (t+b)/2 * h; | ||
| } | ||
|
|
||
| function cylinderArea(param) { | ||
| const [r, h] = check.checkParam(2, param); | ||
| return this.circle([r]) * h | ||
| } | ||
|
|
||
| function getRecursiveCircle(start, end){ | ||
| if(start === end ) {return Math.PI * end * end;} | ||
| return (Math.PI * start * start) + getRecursiveCircle(start+1, end); | ||
| } | ||
|
|
||
| function getArea(figure, ...param){ | ||
| let answer; | ||
| if(figure !== "circle" ) { | ||
| answer = this[figure](param); | ||
| } | ||
| else if(figure === "circle") { | ||
| answer = param.length === 1 ? this[figure](param) : this["getRecursiveCircle"](...check.checkParam(2,param)) | ||
| } | ||
| this.sequence.push([figure, answer]) | ||
| return answer | ||
| } | ||
|
|
||
| function printExecutionSequence(){ | ||
| for (let el of this.sequence) { | ||
| console.log(`호출 함수: ${el[0]} 결과: ${el[1]}`) | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| circle:circleArea, | ||
| rect:rectangleArea, | ||
| trapezoid:trapezoidArea, | ||
| cylinder:cylinderArea, | ||
| getRecursiveCircle, | ||
| getArea, | ||
| printExecutionSequence, | ||
| sequence : [] | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| const check = require('./check'); | ||
|
|
||
| const circleArea = function(...param){ | ||
| const [r] = check.checkParam(1,param); | ||
| return Math.PI * r ** 2; | ||
| } | ||
|
|
||
| const rectancleArea = function(...param){ | ||
| const [width , height] = check.checkParam(2,param); | ||
|
Contributor
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. es2015를 이미 잘 활용하시네요. |
||
| return width * height; | ||
| } | ||
| const trapezoidArea = function(...param){ | ||
| const [base, top, height] = check.checkParam(3,param); | ||
| return (base+top) * height / 2; | ||
| } | ||
| const cylinderArea = function(...param){ | ||
| const [r, height] = check.checkParam(2,param); | ||
| return circleArea(r) * height; | ||
| } | ||
|
|
||
|
|
||
| const getRecursiveCircle = function(start,end){ | ||
| const current = circleArea(start) | ||
| if(start === end) return current; | ||
| else return current + getRecursiveCircle(start+1,end) | ||
| } | ||
|
|
||
| const getArea = function(name,...param){ | ||
| let answer; | ||
| if(name === 'circle' && param.length === 2) { | ||
| answer = getRecursiveCircle(...param); | ||
| } | ||
| else {answer = polygonAreaFunction[name](...param);} | ||
|
|
||
| polygonAreaFunction.sequence.push({name,answer}); | ||
|
|
||
| return answer; | ||
|
|
||
| } | ||
|
|
||
| const printExecutionSequence = function(){ | ||
| polygonAreaFunction.sequence.forEach(el=>{ | ||
| console.log(`호출함수는[${el.name}] 결과는[${el.answer}]`) | ||
| }) | ||
| } | ||
|
|
||
| const polygonAreaFunction = { | ||
| circle:circleArea, | ||
| rect:rectancleArea, | ||
| trapezoid:trapezoidArea, | ||
| cylinder:cylinderArea, | ||
| sequence:[] | ||
| } | ||
|
|
||
| module.exports = { | ||
| getArea, | ||
| printExecutionSequence, | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const numCheck = function (param){ | ||
| param.forEach(el => { | ||
| if(typeof el !== 'number'){ | ||
| throw Error("숫자가 아닌값이 들어왔습니다."); | ||
| } | ||
| }) | ||
| return true | ||
|
|
||
| } | ||
|
|
||
| const checkParamCount = function (count , param){ | ||
| if(count !== param.length) throw Error(` 들어온 인자는 ${param.length}개 이고 들어와야하는 인자는${count}개 입니다.`); | ||
| return true | ||
| } | ||
|
|
||
| const checkParam = function(count,param){ | ||
| return numCheck(param) && checkParamCount(count,param) && param | ||
| } | ||
|
|
||
| module.exports ={ | ||
| checkParam | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
getArea 외에는 export할 필요가 없지 않을까요?