From f088ea684d498e2783b6c708d59a2c1f16d55ab0 Mon Sep 17 00:00:00 2001 From: ymink16 Date: Tue, 9 Apr 2019 16:38:16 +0900 Subject: [PATCH 1/4] step2-2 YM --- areaCalculator.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++ cal.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 areaCalculator.js create mode 100644 cal.js diff --git a/areaCalculator.js b/areaCalculator.js new file mode 100644 index 0000000..1074ab9 --- /dev/null +++ b/areaCalculator.js @@ -0,0 +1,50 @@ +const cal = require("./cal"); +const funcArr = []; +const areaArr = []; + +let getArea = function(shape, ...rest) { + let result = 0; + if (shape === "circle") { + if (rest.length === 1) { + result = cal.getCircleArea(rest[0]); + } else { + result = cal.getCircleSum(rest[0], rest[1]); + } + } else if (shape === "rect") { + result = cal.getSquareArea(rest[0], rest[1]); + } else if (shape === "trapezoid") { + result = cal.getTrapezoidArea(rest[0], rest[1], rest[2]); + } else if (shape === "cylinder") { + result = cal.getCircleArea(rest[0], rest[1]); + } else { + result = "잘못 입력하셨습니다"; + } + + if (result !== "잘못 입력하셨습니다") { + funcArr.push(shape); + areaArr.push(result); + } + return result; +} + +getArea('circle', 2); +getArea('circle', 1, 3); +getArea('rect', 2, 3); +getArea('trapezoid', 5, 8, 10); +getArea('cylinder', 5, 10); +getArea('triple', 4, 3); +console.log(); +console.log(funcArr); +console.log(areaArr); + +let result1 = "계산수행순서 : "; +for (shape of funcArr) { + result1 = result1 + shape + ", "; +} +console.log(result1); +console.log(); + +let result2 = ""; +for (let i = 0; i < funcArr.length; i++) { + console.log(funcArr[i] + " : " + areaArr[i]); +} \ No newline at end of file diff --git a/cal.js b/cal.js new file mode 100644 index 0000000..88a7c88 --- /dev/null +++ b/cal.js @@ -0,0 +1,51 @@ +let getCircleArea = function(r) { + checkErr(r); + return Math.PI * r * r; +} + +let getSquareArea = function(width, height) { + checkErr(width, height); + return width * height; +} + +let getTrapezoidArea = function(top, bottom, height) { + checkErr(top, bottom, height); + return ((top + bottom) * height) / 2 +} + +let getCylinderArea = function(r, height) { + checkErr(r, height); + return getCircleArea(r) * height; +} + +let getCircleSum = function(num1, num2) { + let start, end; + if (num1 >= num2) { + start = num2; + end = num1; + } else { + start = num1; + end = num2; + } + + if (start === end) { + return Math.PI * end * end; + } else { + return Math.PI * end * end + getCircleSum(start, end - 1); + } +} + +let checkErr = function(...args) { + for (let val of args) { + if (typeof val === "undefined") { + throw new Error("인자의 갯수가 부족합니다."); + } + if (typeof val !== "number") { + throw new Error("인자값이 숫자가 아닙니다"); + } + } +} + +module.exports = {getCircleArea, getSquareArea, + getTrapezoidArea, getCylinderArea, getCircleSum}; + \ No newline at end of file From c7f2e05ee418e7f356e8e4e7982454ff73adc604 Mon Sep 17 00:00:00 2001 From: ymink16 Date: Fri, 12 Apr 2019 11:47:46 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EC=A0=84=EC=97=AD=EA=B3=B5=EA=B0=84=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- areaCalculator.js | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/areaCalculator.js b/areaCalculator.js index 1074ab9..163b1ee 100644 --- a/areaCalculator.js +++ b/areaCalculator.js @@ -2,7 +2,7 @@ const cal = require("./cal"); const funcArr = []; const areaArr = []; -let getArea = function(shape, ...rest) { +const getArea = function(shape, ...rest) { let result = 0; if (shape === "circle") { if (rest.length === 1) { @@ -27,24 +27,25 @@ let getArea = function(shape, ...rest) { return result; } -getArea('circle', 2); -getArea('circle', 1, 3); -getArea('rect', 2, 3); -getArea('trapezoid', 5, 8, 10); -getArea('cylinder', 5, 10); -getArea('triple', 4, 3); -console.log(); -console.log(funcArr); -console.log(areaArr); +function main() { + getArea('circle', 2); + getArea('circle', 1, 3); + getArea('rect', 2, 3); + getArea('trapezoid', 5, 8, 10); + getArea('cylinder', 5, 10); + getArea('triple', 4, 3); -let result1 = "계산수행순서 : "; -for (shape of funcArr) { - result1 = result1 + shape + ", "; + let result1 = "계산수행순서 : "; + for (shape of funcArr) { + result1 = result1 + shape + ", "; + } + console.log(result1); + console.log(); + + let result2 = ""; + for (let i = 0; i < funcArr.length; i++) { + console.log(funcArr[i] + " : " + areaArr[i]); + } } -console.log(result1); -console.log(); -let result2 = ""; -for (let i = 0; i < funcArr.length; i++) { - console.log(funcArr[i] + " : " + areaArr[i]); -} \ No newline at end of file +main(); From 6d86379082ba00f2992ff106e0d1e28c9dd45d83 Mon Sep 17 00:00:00 2001 From: ymink16 Date: Fri, 12 Apr 2019 11:54:25 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=ED=95=A8=EC=88=98=ED=91=9C=ED=98=84?= =?UTF-8?q?=EC=8B=9D=20=EC=84=A0=EC=96=B8=EB=B3=80=EA=B2=BD=20let=20->=20c?= =?UTF-8?q?onst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cal.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cal.js b/cal.js index 88a7c88..45290ab 100644 --- a/cal.js +++ b/cal.js @@ -1,24 +1,24 @@ -let getCircleArea = function(r) { - checkErr(r); +const getCircleArea = function(r) { + checkError(r); return Math.PI * r * r; } -let getSquareArea = function(width, height) { - checkErr(width, height); +const getSquareArea = function(width, height) { + checkError(width, height); return width * height; } -let getTrapezoidArea = function(top, bottom, height) { - checkErr(top, bottom, height); +const getTrapezoidArea = function(top, bottom, height) { + checkError(top, bottom, height); return ((top + bottom) * height) / 2 } -let getCylinderArea = function(r, height) { - checkErr(r, height); +const getCylinderArea = function(r, height) { + checkError(r, height); return getCircleArea(r) * height; } -let getCircleSum = function(num1, num2) { +const getCircleSum = function(num1, num2) { let start, end; if (num1 >= num2) { start = num2; @@ -35,13 +35,13 @@ let getCircleSum = function(num1, num2) { } } -let checkErr = function(...args) { +const checkError = function(...args) { for (let val of args) { if (typeof val === "undefined") { throw new Error("인자의 갯수가 부족합니다."); } if (typeof val !== "number") { - throw new Error("인자값이 숫자가 아닙니다"); + throw new Error("인자값이 숫자가 아닙니다."); } } } From 41b72d5188e2e6d73da936d837941cf44a918f07 Mon Sep 17 00:00:00 2001 From: ymink16 Date: Fri, 12 Apr 2019 12:27:19 +0900 Subject: [PATCH 4/4] =?UTF-8?q?arrow=20function=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- areaCalculator.js | 22 +++++++++++++--------- cal.js | 18 +++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/areaCalculator.js b/areaCalculator.js index 163b1ee..39c8683 100644 --- a/areaCalculator.js +++ b/areaCalculator.js @@ -7,7 +7,7 @@ const getArea = function(shape, ...rest) { if (shape === "circle") { if (rest.length === 1) { result = cal.getCircleArea(rest[0]); - } else { + } else if (rest.length === 2) { result = cal.getCircleSum(rest[0], rest[1]); } } else if (shape === "rect") { @@ -27,14 +27,7 @@ const getArea = function(shape, ...rest) { return result; } -function main() { - getArea('circle', 2); - getArea('circle', 1, 3); - getArea('rect', 2, 3); - getArea('trapezoid', 5, 8, 10); - getArea('cylinder', 5, 10); - getArea('triple', 4, 3); - +const printExecutionSequence = function() { let result1 = "계산수행순서 : "; for (shape of funcArr) { result1 = result1 + shape + ", "; @@ -48,4 +41,15 @@ function main() { } } +function main() { + getArea('circle', 2); + getArea('circle', 1, 3); + getArea('rect', 2, 3); + getArea('trapezoid', 5, 8, 10); + getArea('cylinder', 5, 10); + getArea('triple', 4, 3); + + printExecutionSequence(); +} + main(); diff --git a/cal.js b/cal.js index 45290ab..c8170ab 100644 --- a/cal.js +++ b/cal.js @@ -1,24 +1,24 @@ -const getCircleArea = function(r) { - checkError(r); - return Math.PI * r * r; +const getCircleArea = radius => { + checkError(radius); + return Math.PI * radius * radius; } -const getSquareArea = function(width, height) { +const getSquareArea = (width, height) => { checkError(width, height); return width * height; } -const getTrapezoidArea = function(top, bottom, height) { +const getTrapezoidArea = (top, bottom, height) => { checkError(top, bottom, height); return ((top + bottom) * height) / 2 } -const getCylinderArea = function(r, height) { - checkError(r, height); - return getCircleArea(r) * height; +const getCylinderArea = (radius, height) => { + checkError(radius, height); + return getCircleArea(radius) * height; } -const getCircleSum = function(num1, num2) { +const getCircleSum = (num1, num2) => { let start, end; if (num1 >= num2) { start = num2;