Update files

This commit is contained in:
thatblindgeye
2023-01-21 12:53:41 -05:00
parent fb1a2db8d7
commit 4a112362c8
31 changed files with 571 additions and 449 deletions
+23 -23
View File
@@ -1,48 +1,48 @@
const add = function (a, b) {
return a + b;
return a + b;
};
const subtract = function (a, b) {
return a - b;
return a - b;
};
const sum = function (array) {
return array.reduce((total, current) => total + current, 0);
return array.reduce((total, current) => total + current, 0);
};
const multiply = function (array) {
return array.length
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
: 0;
return array.length
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
: 0;
};
const power = function (a, b) {
return Math.pow(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;
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);
if (n === 0) {
return 1;
}
return n * recursiveFactorial(n - 1);
};
module.exports = {
add,
subtract,
sum,
multiply,
power,
factorial,
add,
subtract,
sum,
multiply,
power,
factorial,
};