Tell us what’s happening:
I’m stuck, I’m finding this hard. With the code I’ve written, it’s a potential infinite loop. How can we fix this?
Your code so far
function factorialize(num) {
while (num > 0) {
return num * (num - 1);
}
}
factorialize(5);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
Basic Algorithm Scripting - Factorialize a Number
sanity
2
What’s the base case? When function should stop calling itself, but instead return some known result?
Teller
3
Hi @KoduFCC
You need a way to decrement num
each pass of the loop.
Happy coding
KoduFCC
4
I’m not sure which loop to use, whether it’s a for loop or while loop.
KoduFCC
6
I tried decrementing num, but it’s not working. Here’s my code:
function factorialize(num) {
let fact = 0;
while (num > 0) {
num--;
fact = num * (num - 1);
}
return fact;
}