Factorialize a Number Question Recursion

Hi all,

I peeked into the solution and had a question. I wouldn’t have understood this code snippet so early in my coding career. Could someone help me understand below:

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


I get lost at return num * factorialize(num-1);

How does 5*factorialize(5) gets = 5! ? I don’t understand how recursion happens here.

Anybody have beginner’s guides to point me to? It seems this was a huge step from previous coding challenges.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


So, do you not understand recursion in general, or simply how recursion works in javascript?

It’s not 5 * factorialize(5) it’s 5 * factorialize( 5-1 ), the recursion works because you keep subtracting from the num parameter until you reach zero at which point you return 1 and the answer bubbles up to the original call

so fatorialize(5) == 5 * factorialize(4) == 5 * 4 * factorialize(3) == 5 * 4 * 3 * factorialize(2) == 5 * 4 * 3 * 2 * factorialize(1) == 5 * 4 * 3 * 2 * 1 * factorialize(0) == 5 * 4 * 3 * 2 * 1 * 1

1 Like

I don’t know if this is something that kids do where you grow up, but there’s a game that goes like this.

I ask you “Wanna buy a duck?” and you respond with the question "Does it quack?"
If I am the kid who started the game I say “Of course it quacks!”

Now you have to find someone else. You ask them “Wanna buy a duck?” They ask you “Does it quack?” Now you have to go find me and ask me “Does it quack?” If I started the game I say “Of course it quacks!” If I bought the duck from someone else, I have to find them and ask “Does it quack?” After I tell you “Of course it quacks!” you have to find the kid you’re selling the duck to and say “Of course it quacks!”

The sale is never done until every person in the chain has verified that the duck quacks. We can only “turn around” after we hit the special condition of the kid who started the game.

When we create recursive functions, we return the result of calling the same function (with a modification to the result). The return in our current use of the function can not complete until every layer down the stack is complete. That starts when we hit a call to the function that doesn’t call itself. This is the “base case”. In our duck game it’s the kid who didn’t buy the duck from anyone. In factorials it’s when we are evaluating the factorial of 0. factorialize(0) doesn’t have to call factorialize(-1). It’s the end of the line. That means that it can turn around and the return statement of factorialize(1) can complete…which lets the return statement of fatorialize(2) complete. And so on.

Hi there, I have a question related to the OP, but didn’t feel it necessary to start a new thread.

While I have solved the algorithm with my own solution, i’m having trouble understanding one of the hint answers.

I understand the recursive section( return num * factorialize(num - 1) ). The problem i’m having a hard time understanding is the first part… (if(num === 0) { return 1; } ).

In my head the recursion eventually reduces num to zero, so I cant understand why the algorithm doesn’t just return 1?? How does the function return the factorial sum? Maybe my problem is not understanding how the ‘return’ affects the function?

Any insight would be greatly appreciated.

Thank you.

Let’s take the example of 4 -
The steps followed by the code is -

  1. 4 * factorial (4-1)
  2. 4 * (4-1) * factorial(3-1)
  3. 4 * (4-1) * (3-1) * factorial(2-1)
  4. 4 * (4-1) * (3-1) * 1 * factorial(1-1)
  5. 4 * (4-1) * (3-1) * 1 * 1
  6. 4 * 3 * 2 * 1 * 1

This should clear your doubts.

1 Like

Without (if(num === 0) { return 1; } ) it will recurse infinitely which will eventually crash the browser.

Not sure if you understand my question.

I understand the recursive part. However once the loop reaches 0, the function returns 1… Doesn’t that stop the function and just return 1 instead of the factorialized number?

You are not understanding the recursion. Each time the recursive call is made, it is put on the stack. Once num === 0, then 1 is returned for factorial(0), so the stack can be evaluated in reverse. Once factorial(0) is known, then the previous return 1 * factorial(0) can be evaluated and so forth.

1 Like

More confused than ever. I thought return ends the function?

@nickolaskg It does end the function, but only for the current factorialize() part. If factorialize(1) is called, factorialized (0) will be evaluated first (which returns 1 and end it), then the return value goes to factorialize(1), which hasn’t ended yet, and multiplies it by 1 (1*1).

So basically it goes on something like this:

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

I know the syntax is wrong but hopefully, it can get my point across :sweat_smile:

Okay my whole misunderstanding came from not reading the all the possible challenge steps.

I did not see factorialize(0) should return 1, at the bottom of the list. I was under the impression this affected the rest of the function.

Thank you all for taking the time to respond to my dumb question.