mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 07:29:47 +08:00

* Optimised the factorial function. There was previously an unnecessary check for if the number was 0 or 1. * Create WhileLoopFactorial.test.js The test was not present previously. * result *= num * Update WhileLoopFactorial.test.js * testFactorial function * Space for formatting. * should fix the formatting issues. I was having trouble with npx standard, so I just used the online verifier at https://standardjs.com/demo.html
15 lines
364 B
JavaScript
15 lines
364 B
JavaScript
/*
|
|
author: Theepag, optimised by merelymyself
|
|
*/
|
|
export const factorialize = (num) => {
|
|
// Step 1. Handles cases where num is 0 or 1, by returning 1.
|
|
let result = 1
|
|
// Step 2. WHILE loop
|
|
while (num > 1) {
|
|
result *= num // or result = result * num;
|
|
num-- // decrement 1 at each iteration
|
|
}
|
|
// Step 3. Return the factorial
|
|
return result
|
|
}
|