Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app.js
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())
56 changes: 56 additions & 0 deletions calc-this.js
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 = {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getArea 외에는 export할 필요가 없지 않을까요?

circle:circleArea,
rect:rectangleArea,
trapezoid:trapezoidArea,
cylinder:cylinderArea,
getRecursiveCircle,
getArea,
printExecutionSequence,
sequence : []
}

59 changes: 59 additions & 0 deletions calc.js
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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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,

}
22 changes: 22 additions & 0 deletions check.js
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
}