Basic Algorithms: Using Recursion to Factorialize a Number

Tell us what’s happening:
I understand how to use recursion to take a number and count it down to 1, but I am having an issue figuring out how to multiply each iteration of the number as it is counting down.

I know 5 should output 120, but I am getting 30. What am I missing?

I know there are other threads with this issue but I am not looking for a solution. I am looking to understand this with minimal assistance.

Your code so far


let factorialize = (num) => {
if (num === 0) { return 
} else {
  return num * (num + 1);
  factorialize(num - 1);

return num;
}
}

console.log(factorialize(5));

/* How can I accurately multiply each iteration of num as it is going through recursion? */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0.

Challenge: Factorialize a Number

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number

First, what is the base case and what value should the base case return?

Hint:

factorialize(0); // What should this return?

The base case should be 1, right? Since it needs to stop before 0 (if 0 were included in the factorial the entire product would then be 0).

factorialize(0); // Should return 0 or NaN

0! is 1, so 1 can not be the base case.

So, would 2 be the base case then?
The more I try to read about this issue, the more confused I feel.

The base case is 0, 0! is 1. It is also the case that 1! is 1. So when num === 0, you need to return?

When num === 0 it should return 1
Should I say this?

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

Now any num returns 1

This fixes the previous issue but I am still at 30 when num is returned

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

You don’t really need that, because it will eventually get to the base case and do the same thing, but for now, let’s focus on what should have if num is greater then 1 (based on your code). Also, make sure you are using || and not | in the above code.

So let’s look at factorialize(2). This would need to process something like:

2 * factorialize(1)

and we already have established what factorialize(1) will return.

And then factorialize(3) would process something like:

3 * factorialize(2) * factorialize(1)

Using this processing logic, what would your return statement if num is greater than 1 need to look like, so that you eventually end up calling factorialize(1) (or 0 if you get rid of the OR condition in the if statement)?

I got it! Thank you! I tried something similar and ended up getting “Too much recursion”, but with that logic it works.

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

  return num;
}
}

console.log(factorialize(5)); // Outputs 120

You don’t need this line. Also, you only need an if statement and no else. See if you can do that.

Got it. Thanks for your help!

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

}

console.log(factorialize(5)); // Outputs 120