Tell us what’s happening:
I can’t seem to figure out why this isn’t working. I wrote it out on paper and it seems like it should work correctly?
What I wrote out is this:
num=num*(num-1)
num=5*(5-1)=20
num=20*(4-1)=60
num=60*(3-1)=120
num=120*(2-1)=120
Your code so far
function factorialize(num) {
let a;
for(a=1; num>1; num--){
num*=(num-a);
console.log(num);
}
return num;
}
factorialize(5);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.
Careful, what is changing during your loop?
You are decrementing num by one, but st the same time you are greatly increasing num With the multiplication num > 1 will never be false, you have an infinite loop
You are also using this loop in a weird way, the way you are using variables you would better use a while loop
If you want to use a for loop you would do better creating an iterating variable for that loop for (let i = initialValue; conditionToCheck; change_i)
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
Is this better? I was initially trying to do it this way but I got confused trying to solve it while changing the i variable but I think doing it the way I initially posted and then going back and trying it this way helped a lot :D.