Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Factorialize in JS

Can someone explain to me how this works?

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

factorialize(5);

This is supposed to return 120, 3628800, 2432902008176640000, and 1. Apparantly it works but I just don't understand how.

1 Answer

I'm a bit confused, because the code you linked should only return 120 - none of the other numbers. Maybe there are other calls that aren't in the code you pasted here?

When you run into code that is confusing, it's often helpful to add print statements to the console so you can follow what's happening.

Try running the following code instead, and taking a look at what prints out to the console.

function factorialize(num) {
  if (num === 0 || num === 1) { // If the current num is 0 or 1, return 1.
    console.log("The current value of num is: " + num);
    return 1;
  }
  console.log("The current value of num is: " + num);
  return num * factorialize(num - 1); // Otherwise, return num * (num - 1);
}

console.log("This program will print out the current value of num each time the facrotialize function is called.");
console.log("The function works by multiplying each of these values together recursively.");
console.log("The factorial of 5 is: " + factorialize(5));

Yea I felt the same way but according to freecodecamp.com that's what it's supposed to return. It is a free coding website that I use while I'm at work to continue learning but I do think it's a bit buggy.

So my next question is how does it even return 120? With num being equal to 5... how does num ever at any point equal to 0 or 1? That's very confusing for me.

Thanks,

I would strongly recommend running the above code and looking at the console output. You are calling the factorialize function from within itself, so the program runs recursively. Each time you call the factorialize, the current function is 'put on hold' while the new call executes.

Below I've used comments so the tabs are preserved. Each function is calling the one below it, and then returning that value. The returned value is then used in place of the function call, once it gets back to the 'paused' function call above it.

Hopefully that makes sense. Please let me know if it's still unclear, and I'll do my best to come up with a different way to explain. :)

// factorialize(5) = 5 * factorialize(4) = 5 * 24
  // factorialize(4) = 4 * factorialize(3) = 4 * 6 = 24
    // factorialize(3) = 3 * factorialize(2) = 3 * 2 = 6
      // factorialize(2) = 2 * factorialize(1) = 2 * 1 = 2
        // factorialize(1) = 1