I’m solving the factorial algorithm task but after finish my code, i run the test case for factorial(5) and it gives me 120. but after i put factorial(10) and factorial(20) my browser just crash after few moments. Now i’m trying to remove my code from the task but it gets hang everytime when i get into that task link. Now how can i get back into that code again? help me.
by the way here is my code that makes this trouble:
function factorial(num) {
var result = 1;
for(var i = num; i > 0; i--){
result = num * factorial(num-1);
}
return result;
}
factorial(5);
factorial(10);
factorial(20);
factorial(0);
Finally, I get back into into my code again. i stop the loop process by writing in the url ?run=disable after than i can edit my code again.
BYou need an exit condition before running your recursive loop. I didn’t troubleshoot your code but crashing browser is almost always an endless loop.
For example try adding
If num =1 return num
Before your loop. Yourcode runs a loop on factorial(1) and it doesn’t need to. That should let the recursive loops bubble back up.
Although I don’t know why it would happen on 10 and not on 5
1 Like
yeah, i am also surprise about the factorial(5) test case. But i was than realize i did horrible job. thanks for your reply. i just completed it.
1 Like