Skip to content

Commit 1929509

Browse files
authored
Merge pull request TheAlgorithms#505 from theepag/WhileLoopFactorial
Added while loop factorial
2 parents 9c9640c + 4e680d7 commit 1929509

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Maths/WhileLoopFactorial.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
author: Theepag
3+
*/
4+
const factorialize = (num) => {
5+
// Step 1. variable result to store num
6+
let result = num
7+
// If num = 0 OR 1, the factorial will return 1
8+
if (num === 0 || num === 1) { return 1 }
9+
// Step 2. WHILE loop
10+
while (num > 1) {
11+
num-- // decrement 1 at each iteration
12+
result = result * num // or result = result * num;
13+
}
14+
// Step 3. Return the factorial
15+
return result
16+
}
17+
// test
18+
console.log(factorialize(5))
19+
console.log(factorialize(4))

0 commit comments

Comments
 (0)