forked from TheOdinProject/javascript-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator-solution.js
50 lines (43 loc) · 863 Bytes
/
calculator-solution.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
const add = function (a, b) {
return a + b;
};
const subtract = function (a, b) {
return a - b;
};
const sum = function (array) {
return array.reduce((total, current) => total + current, 0);
};
const multiply = function(...args){
let product = 1;
for (let i = 0; i < args.length; i++) {
product *= args[i];
}
return product;
};
const power = function (a, b) {
return Math.pow(a, b);
};
const factorial = function (n) {
if (n === 0) return 1;
let product = 1;
for (let i = n; i > 0; i--) {
product *= i;
}
return product;
};
// This is another implementation of Factorial that uses recursion
// THANKS to @ThirtyThreeB!
const recursiveFactorial = function (n) {
if (n === 0) {
return 1;
}
return n * recursiveFactorial(n - 1);
};
module.exports = {
add,
subtract,
sum,
multiply,
power,
factorial,
};