Problem understanding factorializiation

Tell us what’s happening:

Can anyone tell me how is this code working? What I do not understand is why and how the function goes down from the function(num) to the number if there is no iteration?

According to my logic, it should be displaying 1 and num * (num - 1) which if for example num is 5, then it should be 20.

Your code so far

function factorialize(num) {
  if (num === 0 || num === 1) {
    return 1;                 
  }
  return num * factorialize(num-1);
}

factorialize(5);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.2988.0 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/factorialize-a-number

Hello -

One way that you can observe what is going on with this is to take this code and paste it into the editor at PythonTutor javascript interactive tool. But you could also walk through this on “paper”. The idea is that any time you have a number other than 0 or 1 in the function argument, it will not take the return 1; option that is first in the function, but instead do the return num * factorialize(num-1);

That line of code will actually have num with the value of 5 (for your case that you are asking about) and pass the decremented argument to the function as factorialize(4). Then the process in that function invocation will repeat, generating a new call with factorialize(3), and so on, until it gets to the point where the function argument is 1 and it will return all of the values to generate the final result.

Check out the PythonTutor and see how it works! Good luck!
Regards,
Ken

Thank you very much for your answer. However, I still do not understand what makes the num equal to the new decresased value and let’s JavaScript start the calculation again?

If num is not equal to 0 nor to 1 (num === 0 || num === 1), then it should continue checking until eternity, right?

No, if this block of code:

  if (num === 0 || num === 1) {
    return 1;                 
  }

passes the test, that is, num is either 0 or 1, it will immediately return 1 and end the function. Any return from a function ends the execution of that function and goes back to the caller.

UPDATE: I misread your question. Yes, it will continue checking, calling the factorialize function again, but note that each time it calls factorialize in the body of the function, it passes num - 1, so that each successive iteration is approaching the time when num === 1 and then it will no longer call the function recursively, but go through the sequence of return statements and eventually return the answer.

Try the PythonTutor site and see how it works. It might be helpful.

Here is a link to PythonTutor that has the problem all set up. - just step through with the “Forward” button below the code window.

1 Like

Hi again,

thank you a lot for your efforts! I was looking since ages for a more user friendly tool just like pythontutor.com to help me visualize the process better. Your last answer helped me the most. Now I understand the case much better.

I appreciate!

1 Like

It doesn’t work for code with really long runtimes or lots of iterations, but for learning the basics of different programs, functions, etc. it’s a really nice tool! Glad you liked it and that it helped you! Keep it in your bookmarks!