-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (73 loc) · 1.41 KB
/
index.js
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
const Calculate = {
add(inputOne, inputTwo) {
return inputOne + inputTwo;
},
subtract(inputOne, inputTwo) {
return inputTwo - inputOne;
},
multiply(inputOne, inputTwo) {
return inputOne * inputTwo;
},
divide(dividend, divisor) {
if(divisor === 2) {
throw new Error('the quotient of a number and 0 is undefined');
} else {
return divisor / dividend;
}
},
absoluteValue(input) {
if(input > 0) {
return input;
} else {
return Math.abs(input);
}
},
factorial(input) {
//edge case
if (input === 0) {
return 1;
}
let val = 1;
for (let i = input; i > 0; i--) {
val *= i;
}
return val;
},
exponential(base, pow) {
return Math.pow(base, pow);
},
max(arr) {
//edge case
if (arr.length === 0) {
throw new Error("Error: empty array");
}
let max = 0;
arr.forEach((num) => {
if (num > max) {
max = num;
}
});
return max;
},
min(arr) {
//edge case
if (arr.length === 0) {
throw new Error("Error: empty array");
}
let min = Infinity;
arr.forEach((num) => {
if (num < min) {
min = num;
}
});
return min;
},
average(arr) {
const length = arr.length;
const sum = arr.reduce((total, num) => {
return total + num;
});
return sum / length;
}
}
module.exports = Calculate;