# Factorial Fun explainer

The aim of this puzzle: Use a function that calls itself recursively.

Walkthrough of the solution:
The function `factorial()` takes a number as an argument, and returns the product of every number up to that number. For example, `factorial(5)` will return 120, as `5 * 4 * 3 * 2 * 1 = 120`.

To complete the puzzle, change the code block inside the else statement from `return number * factorial(number)` to `return number * factorial(number - 1)`.

Let’s take a look inside the function to see how it works.

Inside the function is an if…else statement. The if statement tests if the number passed into the function is less than or equal to 1. If this is true, the function will return 1. However, if the number is greater than 1, the code in the else block will run.

Inside the else block is where things get a bit tricky. The function will take the number, and multiply it by the result of calling the function again, this time with `number - 1` as an argument.

This will make more sense if we follow a number through the function. Let’s try calling the function with `3` as an argument: `factorial(3)`:

The if statement will check if 3 is less than or equal to 1. Because 3 is greater than 1, the code in the else block will run instead. This will return `3 * factorial(2)`.

The equation is now `3 * factorial(2)`.

The if statement will check the number again to see if 2 is less than or equal to 1. This is still false, so the else block will run, this time returning `2 * factorial(1)`, calling the function again with `1` as the number argument.

Our equation is now `3 * 2 * factorial(1)`

When the function runs again with `1` as the number, the if statement code block will run, because 1 is less than or equal to 1. This code block simply returns the number, and the function can stop calling itself. This is important, as calling a function recursively without a condition that stops the function will make the recursion endless, and will cause a program to crash. `sumUpTo(1)` returns `1`, and stops the function.

Our final equation is `3 * 2 * 1`, which equals `6`.

It can also be visualized like this:

``````factorial(3) → (3 * factorial(2))
factorial(2) → (2 * factorial(1))
factorial(1) → 1
``````

Which simplifies to:

``````factorial(3) → (3 * (2 * (1) ) )
``````

Sample code solution:
(Tap below to reveal)

``````function factorial(number) {
if (number <= 1) {
return 1;
} else {
return number * factorial(number - 1);
}
}

console.log(factorial(5));
``````

JavaScript Concepts: Recursion, Functions, Control Flow, If…Else Statements

8 Likes

Hey there!
I really enjoy your app. I have a question:

Where does equality come from? I thought < symbol meant “less than”, so that it should have continued with factorial(1) ->
1*factorial(0)->
1

Good catch! That looks like a mistake. I’ll update the explainer.

Thanks!
Ben

In other programming languages I’ve used, If statements are only evaluated once, unless they’re nested into a for-next loop.

Since there is a factorial function, why is the answer not simply
Return factorial(number)

Hey there, the if statement is only running once, but the function is running multiple times.

This function uses recursion, meaning it calls itself over and over until a certain condition is true. Every time the function is called, the if statement is evaluated.

The call stack (all of the function calls in a queue) for calling the function looks like this (newest calls at top, oldest calls at the bottom):

``````factorial(1) // returns 1
factorial(2) // returns 2 * factorial(1)
factorial(3) // returns 3 * factorial(2)
factorial(4) // returns 4 * factorial(3)
factorial(5) // returns 5 * factorial(4)
``````

Hope that makes sense! Let me know if you have any questions.
Ben

3 Likes

Thanks for getting back to me. Got it. I shouldn’t look at code past midnight!

1 Like

Ok thanks it took a while to remember.

What is the problem

I don’t get it too,this puzzle my be ment to send us to this page.

(to @Abundant_Lifestyle also):

You don’t want to put

`factorial(number)`

inside the `if` conditional, because when you do it the code run tries to evaluate it to decide if the condition is valid. So, to evaluate it, the code run will obviously have to call `factorial` function again, thus creating an eternal loop (calling `factorial`, again and again, whenever code run reaches that first `if` line of the function again and again…)

Instead, you want to use some deterministic code inside `if`'s conditional, as that way the program won’t be taking the risk of not being able of evaluating it on the run, and the condition, evaluated, will provide for the code run to proceed.
For deterministic code I mean some code already determined, like `(number <= 1)`. That’s a valid and determined condition, as “`number`” has already been passed to the function as an argument (it is equal to 5 in the 1st run), and “1” is a pure number.

As for `factorial(number - 1)`, try using it in the value being returned by the function for arguments higher than `1`, as in:

``````return number * factorial(number - 1);
``````

That way, the function code logic will be right for the `factorial` function

1 Like