-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (86 loc) · 2.38 KB
/
index.js
File metadata and controls
96 lines (86 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var countCal = 0;
var orderCal = [];
function checkEvery(targetArr, funcName) {
for (let i = 0; i < targetArr.length; i++) {
if (!funcName(targetArr[i])) {
return false;
}
}
return true;
}
function getNumberOfInput(targetArr) {
let rmUndefined = targetArr.filter(elem => typeof elem !== 'undefined');
let result = rmUndefined.length
return result;
}
function checkInputValue(inputValArray, { expectedValue }) {
let getNumberOfValidInput = getNumberOfInput(inputValArray);
if (!getNumberOfValidInput) {
throw "최소 한가지 값이 필요합니다.";
}
if (getNumberOfValidInput !== expectedValue) {
throw `${expectedValue}개의 인자가 필요합니다.`;
}
if (!checkEvery(inputValArray, elem => typeof elem === 'number')) {
throw "숫자형 타입만 계산이 가능합니다.";
}
if (!checkEvery(inputValArray, elem => elem > 0)) {
throw "입력값은 0보다 커야 합니다.";
}
}
function getReport(collectValue) {
let result;
if (collectValue) {
countCal++;
orderCal.push(collectValue);
result = `계산이 ${countCal}번 일어났습니다.`;
}
else {
result = '계산수행순서 : ' + orderCal.join(', ');
console.log(result);
}
return result;
}
function repeatCalculateCircle(radius, repeat) {
for (let i = 0; i < repeat; i++) {
calculateCircle(radius + i);
}
}
function calculateCircle(radius) {
checkInputValue([radius], { expectedValue: 1 });
let result = radius * radius * Math.PI;
console.log(result);
console.log(getReport('circle'));
}
function calculateSquare(width, height) {
checkInputValue([width, height], { expectedValue: 2 });
let result = width * height;
console.log(result);
console.log(getReport('rect'));
}
function calculateTrapezoid(upper, lower, height) {
checkInputValue([upper, lower, height], { expectedValue: 3 });
let result = (upper + lower) * height / 2;
console.log(result);
console.log(getReport('trapezoid'));
}
function getArea(shape, v1, v2, v3) {
switch (shape) {
case 'circle':
if (!v2) calculateCircle(v1);
else repeatCalculateCircle(v1, v2);
break;
case 'rect':
calculateSquare(v1, v2);
break;
case 'trapezoid':
calculateTrapezoid(v1, v2, v3);
break;
}
}
calculateCircle(1);
calculateCircle(2);
getArea('circle', 2);
getArea('rect', 10, 15);
getArea('trapezoid', 10, 15, 12);
getReport();