8000 Update calculator.js · M-Munk/javascript-exercises@d63ee47 · GitHub
[go: up one dir, main page]

Skip to content

Commit d63ee47

Browse files
authored
Update calculator.js
1 parent 83a2b6a commit d63ee47

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

calculator/calculator.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,20 @@ function power(a, b) {
1919
}
2020

2121
function factorial(n) {
22+
if (n == 0) return 1;
23+
let product = 1;
24+
for (let i = n; i > 0; i--) {
25+
product *= i;
26+
}
27+
return product;
28+
}
29+
30+
// This is another implementation of Factorial that uses recursion
31+
// THANKS to @ThirtyThreeB!
32+
function recursiveFactorial(n) {
2233
if (n===0){
2334
return 1;
24-
}
35+
}
2536
return n * factorial (n-1);
2637
}
2738

0 commit comments

Comments
 (0)
0