diff --git a/app.js b/app.js new file mode 100644 index 0000000..507c694 --- /dev/null +++ b/app.js @@ -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()) \ 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 new file mode 100644 index 0000000..b0eef32 --- /dev/null +++ b/calc.js @@ -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); + 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, + +} diff --git a/check.js b/check.js new file mode 100644 index 0000000..4eea6b9 --- /dev/null +++ b/check.js @@ -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 +} \ No newline at end of file