# Factorialize a Number

Learn how to solve the freeCodeCamp algorithm 'Factorialize a Number' using a little bit of recursion.

The requirement for this freeCodeCamp algorithm is to return the factorial of the provided number, in this case `num`.

We know that only integers greater than or equal to `0` will be be supplied.

## Test Cases

• `factorialize(5)` should return a `Number`
• `factorialize(5)` should return `120`
• `factorialize(10)` should return `3628800`
• `factorialize(20)` should return `2432902008176640000`
• `factorialize(0)` should return `1`

## Psuedocode

Let's break the problem into smaller chunks.

``````// Take a given number
// If number equals zero
// Return 1
// Otherwise
// Take number and multiply it
// By itself minus 1 until
// Number reaches 0

``````

Hint: Look into using recursion to solve this problem.

## Solving the Algorithm

### Setting up our function

``````function factorialize(num) {

};
``````

### Add edge case for input of '0'

We know that if there's an input of `0`, we will need to return `1`, so let's create an if statement to check for this.

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

Now that we can check the input for `0`, let's create the remaining logic for all other positive integers.

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

// Using recursive method
return num * factorialize(num - 1);
// First iteration: 5 * (5-1) => 20
// Second iteration: 20 * (4-1) => 60
// Third iteration: 60 * (3-1) => 120
// Fourth iteration: 120 * (2-1) => 120
};

factorialize(5) // Outputs: 120
``````

## Final Thoughts

Recursion is certainly one of the more confusing aspects in computer programming, so don't feel bad if you don't entirely understand it at this point. Just know that it's essentially a function that calls on itself, such as the solution above which calls itself using `num - 1`. Hopefully you found this to be a helpful walkthrough of this freeCodeCamp algorithm on how to factorialize a number!

Shoot me an email at `[email protected]` with any questions and if you enjoyed this, stay tuned and subscribe below! 👇

Help us improve our content