## Algorithm 02: Factorialize a Number

In this algorithm we will see how to take a number, then find its factorial. If you forgot from your math class what a factorial is it is the product of an integer and all the integers below. For example, the factorial of 5 would be: 5! = 1 * 2 * 3 * 4 * 5 = 120.

One way factorials are useful in the real world would be to find out how many different ways a set number of objects can be arranged. Back to our example, if we have a group of 5 people and we want to find all the different combinations of how we can line them up in a row, there would be 5! = 120 different ways they could be lined up.

1 2 3 4 5 6 7 8 9 10 11 12 | // Factorialize a Number // For example: 5! = 1 * 2 * 3 * 4 * 5 = 120 const factorialize = (num) => { if (num === 0){ return 1; } else { return num * factorialize( num - 1 ); // recursion } } factorialize(5); // returns 120 |

The key to solving this algorithm is recursion. We call the same function over and over again until we meet the condition that num === 0.