I came across an exercise on freecodecamp that required writing a code that would return the factorial for a given integer and gave this example: For example: 5! = 1 * 2 * 3 * 4 * 5 = 120f.
I get how the math works but I couldn't really wrap my head around how to code it until I found something here, n stackoverflow, but without an explination of why it works, that resembled this:
function factorialize(num) {
if(num === 0) {
return 1;
} else {
return num * factorialize(num - 1);
}
}
factorialize(5);
I don't really understand how this is iterating through all the integers that are less than or equal to num. Can anybody help explain this to me?