2017-12-15 18:56:14 +00:00
|
|
|
function add(a, b) {
|
|
|
|
return a + b;
|
2017-09-20 23:04:46 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:56:14 +00:00
|
|
|
function subtract(a, b) {
|
|
|
|
return a - b;
|
2017-09-20 23:04:46 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:56:14 +00:00
|
|
|
function sum(array) {
|
2019-05-16 02:49:12 +00:00
|
|
|
return array.reduce((total, current) => total + current, 0);
|
2017-09-20 23:04:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:35:40 +00:00
|
|
|
function multiply(array) {
|
2018-09-27 06:18:29 +00:00
|
|
|
return array.length
|
2018-09-28 05:38:36 +00:00
|
|
|
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
|
2018-09-28 05:36:03 +00:00
|
|
|
: 0;
|
2017-09-20 23:04:46 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:56:14 +00:00
|
|
|
function power(a, b) {
|
|
|
|
return Math.pow(a, b);
|
2017-09-20 23:04:46 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:56:14 +00:00
|
|
|
function factorial(n) {
|
2018-01-03 18:21:25 +00:00
|
|
|
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!
|
|
|
|
function recursiveFactorial(n) {
|
2018-01-03 17:10:49 +00:00
|
|
|
if (n===0){
|
|
|
|
return 1;
|
2018-01-03 18:21:25 +00:00
|
|
|
}
|
2018-08-19 20:02:55 +00:00
|
|
|
return n * recursiveFactorial (n-1);
|
2017-09-20 23:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-12-15 18:56:14 +00:00
|
|
|
add,
|
|
|
|
subtract,
|
|
|
|
sum,
|
|
|
|
multiply,
|
|
|
|
power,
|
|
|
|
factorial
|
|
|
|
};
|