From bcd7fa7a32d2f1f4d8d0f8bc3028c22a01a6c5fc Mon Sep 17 00:00:00 2001 From: Soojin Roh Date: Tue, 9 Apr 2019 18:35:23 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A6=AC=EB=B7=B0?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Math.pow를 중복해서 호출 사용하던 것을 const{pow} = Math 로 비구조화할당하여 사용하는 것으로 수정하였습니다. 2. 원기둥을 구하는 함수 cylinderArea()에서 원의 넓이를 구하는데 circleArea() 함수를 재사용하는 것으로 수정하였습니다. --- STEP2_1.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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)); From 15562af25221f22f08c90d0e922190d8a185b5f9 Mon Sep 17 00:00:00 2001 From: Soojin Roh Date: Thu, 11 Apr 2019 17:11:18 +0900 Subject: [PATCH 2/2] =?UTF-8?q?190411=20JS=20Step=202-2=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.getArea()함수 구현 2.printExecutionSequence() 함수 구현 3.printExecutionSequence() 에서 함수의 결과까지 같이 출력하도록 수정 --- STEP2-2/STEP2_2.js | 32 +++++++++++++++++++++ STEP2-2/STEP2_2_utils.js | 61 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 STEP2-2/STEP2_2.js create mode 100644 STEP2-2/STEP2_2_utils.js 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 +}