Files
odin-javascript-exercises/08_calculator/solution/calculator-solution.js
T
Nikita Revenco 930673d9c8 feat(08): remove recursive solution (#457)
Not appropriate to expose learners to at this point in the curriculum
2024-05-03 02:13:38 +01:00

38 lines
602 B
JavaScript

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 (array) {
return array.reduce((product, current) => product * current)
};
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;
};
module.exports = {
add,
subtract,
sum,
multiply,
power,
factorial,
};