Hello,
Can someone point me to the right direction please? I’m trying to understand what’s happening below:
function factorialize(num) {
for (var i = 1; num >= 1; num--) {
i = num * i;
}
return i;
}
factorialize(5);
What I understand is:
I set i to 1; if num is the same or bigger than 1, perform this loop; decrease num by 1 every time loop runs
I understand this line (i think so) but I’m struggling with the second line:
i = num * i;
I know loop needs to run 5 times because our num number is 5.
After the first loop, is:
i = num * i; the same as i = 5 * i; ?
or is:
i = num * i; the same as i = 4 * i; ?
I’m confused because the first line of my loop says that it will decrease num by 1 every time the loop runs so if I run:
for (var i = 1; num >= 1; num–) {
i = num * 1; num on this line should be 4 because i have decreased it by 1 but then it would not make sense
}
I just don’t understand why on the second line the num is still 5, not 4, after the first loop.
I hope you understand me. If not, please let me know
Thank you for help.