odin-javascript-exercises/calculator/calculator.js
seoul2045 645cbc746f
Update calculator.js
The reduce() method syntax for callbacks assigns the accumulator value followed by the currentValue, so I think the above variables should be switched.
2019-05-16 09:49:12 +07:00

48 lines
786 B
JavaScript

function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function sum(array) {
return array.reduce((total, current) => total + current, 0);
}
function multiply(array) {
return array.length
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
: 0;
}
function power(a, b) {
return Math.pow(a, b);
}
function factorial(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!
function recursiveFactorial(n) {
if (n===0){
return 1;
}
return n * recursiveFactorial (n-1);
}
module.exports = {
add,
subtract,
sum,
multiply,
power,
factorial
};