diff --git a/STEP2-2/STEP2_2.js b/STEP2-2/STEP2_2.js new file mode 100644 index 0000000..c02e3df --- /dev/null +++ b/STEP2-2/STEP2_2.js @@ -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); + 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(); \ No newline at end of file diff --git a/STEP2-2/STEP2_2_utils.js b/STEP2-2/STEP2_2_utils.js new file mode 100644 index 0000000..008a8e9 --- /dev/null +++ b/STEP2-2/STEP2_2_utils.js @@ -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 +} diff --git a/STEP2_1.js b/STEP2_1.js index be34d72..a3ea1b0 100644 --- a/STEP2_1.js +++ b/STEP2_1.js @@ -1,4 +1,5 @@ const {PI} = Math +const {pow} = Math function checkType(arr) { for (let i = 0; i < arr.length; i++) { @@ -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) { @@ -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));