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
32 changes: 32 additions & 0 deletions STEP2-2/STEP2_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let utils = require("./STEP2_2_utils")
let log = "<계산수행순서 > \n";
let result = 0;

let getArea = (func_name, ...args) => {
switch (func_name){
case 'circle':
result = utils.polygon.circle(...args);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

오~ 앞의 ...과 뒤의 ... 다 적절한 사용인 듯 합니다 👍

break;
case 'rect':
result = utils.polygon.rect(...args);
break;
case 'trapezoid':
result = utils.polygon.trapezoid(...args);
break;
case 'cylinder':
result = utils.polygon.cylinder(...args);
break;
}
log += `${func_name} : ${result} \n`;
};

let printExecutionSequence = () => {
console.log(log.substr(0, log.length - 2))
}

getArea('circle', 3)
getArea('circle', 1, 3);
getArea('rect', 10, 15);
getArea('trapezoid', 10, 15, 12);
getArea('cylinder', 3, 7);
printExecutionSequence();
61 changes: 61 additions & 0 deletions STEP2-2/STEP2_2_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const {PI} = Math
const {pow} = Math
let sum = 0;
let initSum = 0;

let checkType = function(arr) {
for (let i = 0; i < arr.length; i++) {
if (typeof(arr[i]) !== "number") {
throw "please input Number data type";
}
}
}

let checkArgumentCount = function(arr, correctCount) {
if (arr.length !== correctCount) {
throw "please input correct number of arguments"
}
}



let circleArea = function (...args) {
args[1] = args[1] || args[0];

checkArgumentCount(args, 2);

checkType(arguments);

sum += PI * pow(args[1], 2);
if (args[0] == args[1]) {
initSum = sum
sum = 0
return initSum
}
return circleArea(1, args[1]-1);
}

let squareArea = function(width, height) {
checkArgumentCount(arguments, 2);
checkType(arguments);
return width * height;
}

let trapezoidArea = function(x1, x2, h) {
checkArgumentCount(arguments, 3);
checkType(arguments);
return (x1 + x2) * h / 2
}

let cylinderArea = function(r, h) {
checkArgumentCount(arguments, 2);
checkType(arguments);
return (2 * circleArea(r)) + 2 * PI * r * h
}

exports.polygon = {
'circle' : circleArea,
'rect' : squareArea,
'trapezoid' : trapezoidArea,
'cylinder' : cylinderArea
}
5 changes: 3 additions & 2 deletions STEP2_1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {PI} = Math
const {pow} = Math

function checkType(arr) {
for (let i = 0; i < arr.length; i++) {
Expand All @@ -18,7 +19,7 @@ function circleArea(r) {
checkArgumentCount(arguments, 1);
checkType(arguments);

return PI * Math.pow(r, 2)
return PI * pow(r, 2)
}

function squareArea(width, height) {
Expand All @@ -39,7 +40,7 @@ function cylinderArea(r, h) {
checkArgumentCount(arguments, 2);
checkType(arguments);

return (2 * PI * Math.pow(r, 2)) + 2 * PI * r * h
return (2 * circleArea(r)) + 2 * PI * r * h
}

console.log(circleArea(3));
Expand Down