mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-07-13 19:56:05 +09:00
Merge branch 'solutions' into master
This commit is contained in:
+35
-19
@@ -1,32 +1,48 @@
|
||||
function add () {
|
||||
|
||||
function add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
function subtract () {
|
||||
|
||||
function subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
function sum () {
|
||||
|
||||
function sum(array) {
|
||||
return array.reduce((total, current) => total + current, 0);
|
||||
}
|
||||
|
||||
function multiply () {
|
||||
|
||||
function multiply(array) {
|
||||
return array.length
|
||||
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
|
||||
: 0;
|
||||
}
|
||||
|
||||
function power() {
|
||||
|
||||
function power(a, b) {
|
||||
return Math.pow(a, b);
|
||||
}
|
||||
|
||||
function factorial() {
|
||||
|
||||
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
|
||||
}
|
||||
add,
|
||||
subtract,
|
||||
sum,
|
||||
multiply,
|
||||
power,
|
||||
factorial
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user