From 940f186c72510b5f56fe43fdc002caa31aa419fb Mon Sep 17 00:00:00 2001 From: shun Date: Tue, 9 Apr 2019 18:46:25 +0900 Subject: [PATCH 1/2] feat: add polygon-calculator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit app.js - 실행 파일 calc.js - 계산 관련 함수 check.js - validate 관련 함수 --- app.js | 16 ++++++++++++++++ calc.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ check.js | 22 ++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 app.js create mode 100644 calc.js create mode 100644 check.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..798d994 --- /dev/null +++ b/app.js @@ -0,0 +1,16 @@ +const calc = require('./calc'); + +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)); diff --git a/calc.js b/calc.js new file mode 100644 index 0000000..6740887 --- /dev/null +++ b/calc.js @@ -0,0 +1,49 @@ +const check = require('./check'); + +circleArea = function(...param){ + const [r] = check.checkParam(1,param); + return Math.PI * r ** 2; + +} + +rectancleArea = function(...param){ + const [width , height] = check.checkParam(2,param); + return width * height; +} +trapezoidArea = function(...param){ + const [base, top, height] = check.checkParam(3,param); + return (base+top) * height / 2; +} +cylinderArea = function(...param){ + const [r, height] = check.checkParam(2,param); + return circleArea(r) * height; +} + + +getRecursiveCircle = function(start,end){ + const current = circleArea(start) + if(start === end) return current; + else return current + getRecursiveCircle(start+1,end) +} + +getArea = function(name,...param){ + switch(name){ + case 'circle': + if(param.length === 2) return getRecursiveCircle(...param); + return circleArea(...param); + + case 'rect': + return rectancleArea(...param); + + case 'trapezoid': + return trapezoidArea(...param); + + case 'cylinder': + return cylinderArea(...param); + + } +} + +module.exports = { + getArea +} diff --git a/check.js b/check.js new file mode 100644 index 0000000..eedac08 --- /dev/null +++ b/check.js @@ -0,0 +1,22 @@ +numCheck = function (param){ + param.forEach(el => { + if(typeof el !== 'number'){ + throw Error("숫자가 아닌값이 들어왔습니다."); + } + }) + return true + +} + +checkParamCount = function (count , param){ + if(count !== param.length) throw Error(` 들어온 인자는 ${param.length}개 이고 들어와야하는 인자는${count}개 입니다.`); + return true +} + +checkParam = function(count,param){ + return numCheck(param) && checkParamCount(count,param) && param +} + +module.exports ={ + checkParam +} \ No newline at end of file From 63c14c36fc416720405af0c4694bd8770709a4c6 Mon Sep 17 00:00:00 2001 From: shun Date: Thu, 11 Apr 2019 20:03:36 +0900 Subject: [PATCH 2/2] feat&refactor : add printExecutionSequence , change switch -> object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sequence 변수를 만들고 getArea에서 push를 하고 나중에 출력하도록 하였습니다. 이때 sequence 변수는 객체의 속성으로 정의하였습니다. switch문 대신에 polygonAreaFunction 객체를 만들고 getArea에서 입력받은 문장을 key값으로 , value는 함수를 연결하였습니다. 출력해주는 printExecutionSequence 함수에서 값의 변경을 용이하게 하기 위해 객체로 값을 넘겨주었습니다. --- app.js | 5 +++++ calc-this.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ calc.js | 54 +++++++++++++++++++++++++++++--------------------- check.js | 6 +++--- 4 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 calc-this.js diff --git a/app.js b/app.js index 798d994..507c694 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,6 @@ const calc = require('./calc'); +// const calc = require('./calc-this'); + const {log} = console; @@ -14,3 +16,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()) \ No newline at end of file diff --git a/calc-this.js b/calc-this.js new file mode 100644 index 0000000..a48d8f6 --- /dev/null +++ b/calc-this.js @@ -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 : [] + } + diff --git a/calc.js b/calc.js index 6740887..b0eef32 100644 --- a/calc.js +++ b/calc.js @@ -1,49 +1,59 @@ const check = require('./check'); -circleArea = function(...param){ +const circleArea = function(...param){ const [r] = check.checkParam(1,param); return Math.PI * r ** 2; - } -rectancleArea = function(...param){ +const rectancleArea = function(...param){ const [width , height] = check.checkParam(2,param); return width * height; } -trapezoidArea = function(...param){ +const trapezoidArea = function(...param){ const [base, top, height] = check.checkParam(3,param); return (base+top) * height / 2; } -cylinderArea = function(...param){ +const cylinderArea = function(...param){ const [r, height] = check.checkParam(2,param); return circleArea(r) * height; } -getRecursiveCircle = function(start,end){ +const getRecursiveCircle = function(start,end){ const current = circleArea(start) if(start === end) return current; else return current + getRecursiveCircle(start+1,end) } -getArea = function(name,...param){ - switch(name){ - case 'circle': - if(param.length === 2) return getRecursiveCircle(...param); - return circleArea(...param); - - case 'rect': - return rectancleArea(...param); - - case 'trapezoid': - return trapezoidArea(...param); - - case 'cylinder': - return cylinderArea(...param); - +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 + getArea, + printExecutionSequence, + } diff --git a/check.js b/check.js index eedac08..4eea6b9 100644 --- a/check.js +++ b/check.js @@ -1,4 +1,4 @@ -numCheck = function (param){ +const numCheck = function (param){ param.forEach(el => { if(typeof el !== 'number'){ throw Error("숫자가 아닌값이 들어왔습니다."); @@ -8,12 +8,12 @@ numCheck = function (param){ } -checkParamCount = function (count , param){ +const checkParamCount = function (count , param){ if(count !== param.length) throw Error(` 들어온 인자는 ${param.length}개 이고 들어와야하는 인자는${count}개 입니다.`); return true } -checkParam = function(count,param){ +const checkParam = function(count,param){ return numCheck(param) && checkParamCount(count,param) && param }