It is very easy to make mistakes when you have to handle variables, change them, keep track, etc. Especially in a loop. A great way to understand what's going on is to use the simplest debugging technique —
console.log. This function prints onto the screen whatever you pass to it.
I have a function that iteratively calculates the factorial. The idea is simple: multiply numbers from 1 up to n.