From 5b35b4bff2f5a0267fe5bf3a000ed3b925bbcaf3 Mon Sep 17 00:00:00 2001 From: Soojin Roh Date: Mon, 8 Apr 2019 17:43:06 +0900 Subject: [PATCH] =?UTF-8?q?step2=5F1=20=EB=8B=A4=EA=B0=81=ED=98=95?= =?UTF-8?q?=EC=9D=98=20=EB=84=93=EC=9D=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 반지름을 입력받아 원의 넓이를 계산하는 함수 2. 사각형의 넓이를 계산하는 함수 3. 사다리꼴의 넓이를 계산하는 함수 4. 원기둥의 넓이를 계산하는 함수 위의 네 개의 함수를 구현하고 숫자가 아닌 경우, 인자의 갯수가 틀린 경우 에러를 반환합니다. --- STEP2_1.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 STEP2_1.js diff --git a/STEP2_1.js b/STEP2_1.js new file mode 100644 index 0000000..be34d72 --- /dev/null +++ b/STEP2_1.js @@ -0,0 +1,48 @@ +const {PI} = Math + +function checkType(arr) { + for (let i = 0; i < arr.length; i++) { + if (typeof(arr[i]) !== "number") { + throw "please input Number data type"; + } + } +} + +function checkArgumentCount(arr, correctCount) { + if (arr.length !== correctCount) { + throw "please input correct number of arguments" + } +} + +function circleArea(r) { + checkArgumentCount(arguments, 1); + checkType(arguments); + + return PI * Math.pow(r, 2) +} + +function squareArea(width, height) { + checkArgumentCount(arguments, 2); + checkType(arguments); + + return width * height; +} + +function trapezoidArea(x1, x2, h) { + checkArgumentCount(arguments, 3); + checkType(arguments); + + return (x1 + x2) * h / 2 +} + +function cylinderArea(r, h) { + checkArgumentCount(arguments, 2); + checkType(arguments); + + return (2 * PI * Math.pow(r, 2)) + 2 * PI * r * h +} + +console.log(circleArea(3)); +console.log(squareArea(2, 7)); +console.log(trapezoidArea(6, 3, 3)); +console.log(cylinderArea(4, 6)); \ No newline at end of file