Skip to content

Commit e6622d8

Browse files
committed
Add factorial example
1 parent 8cdcf39 commit e6622d8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

7-recursion/factorial.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Non-recursive solution (Iterative solution)
2+
const factorial = (num) => {
3+
let total = 1;
4+
5+
for (let i = num; i > 1; i--) {
6+
total *= i;
7+
}
8+
9+
return total;
10+
};
11+
12+
console.log(factorial(10));
13+
14+
// Recursive solution
15+
16+
const factorialRe = (num) => {
17+
if (num === 1) {
18+
return 1;
19+
}
20+
21+
return num * factorialRe(num - 1);
22+
};
23+
24+
console.log(factorialRe(10));

0 commit comments

Comments
 (0)