 # Factorialize a number algorithm

## I’m on the Factorialize a Number quiz. I tried a few times then looked up hints and tried to understand their presented algorithm.I don’t understand why it works. Explain like i;'m five if possible.

``````function factorialize(num) {
for( i = 1;num >= 1 ; num--) {
i = num * i;
}
return i;
}

factorialize(5);
``````

I modified the above solution with some console.log statements, so you can see the value of the variables at each step of the way. Also, I have blurred your solution, so as not to spoil the solution for others.

``````function factorialize(num) {
for(i = 1; num >= 1 ; num--) {
console.log('start of iteration i = ' + i);
console.log('num = ' + num);
i = num * i;
console.log('after reassigning num * i to i,  i =  '+i);
console.log(); // line break to separate iterations
}
return i;
}

factorialize(5);
``````
``````The output of the above is:
> start of iteration i = 1
> num = 5
> after reassigning num * i to i, i =  5
>
> start of iteration i = 5
> num = 4
> after reassigning num * i to i, i =  20
>
> start of iteration i = 20
> num = 3
> after reassigning num * i to i, i =  60
>
> start of iteration i = 60
> num = 2
> after reassigning num * i to i, i =  120
>
> start of iteration i = 120
> num = 1
> after reassigning num * i to i, i =  120
>
> => 120``````