Photo by Justin Campbell on Unsplash

# Factorial for the Real World: How to Find Factorial of Numbers in JavaScript.

## Learn how to understand Factorials for the real world applications

### Table of contents

- What is a Factorial?
- Computing factorial with maths notation
- Computing factorial with JavaScript
- factorial using for-loop
- computing factorial using while loop
- Step 1: Create the result storage, position and the stopping point
- Step 2: Multiply the number and its subsequent numbers repetitively once the number is great than 1.
- Step 3: Return the result once the iteration is successful
- Pros of using iteration to compute factorial:
- Cons of using iteration to compute factorial:

- Computing Factorial using Recursion
- Using factorial in the real world
- Conclusion

Welcome to this tutorial, in this tutorial, we will discuss how to find factorial of numbers using JavaScript. We will try as much as possible to make sure we address how to use factorial in the real world. So, what is a Factorial?

## What is a Factorial?

Click the image below to watch its video version:

It is "the product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24." -- source. In the real world, a Factorial refers to the total number of positions the items in a group can occupy in relation to their order of selection.

For example, If you have 5 items or 5 as a number, you would have:

```
5! = 5 * 4 * 3 * 2 * 1 = 120
```

It could also be:

Order of selection | Number of possible positions |

1st | 5 |

2nd | 4 |

3rd | 3 |

4th | 2 |

5th | 1 |

Total | 120 |

If you think about the table above, you would realize we are dealing with two things per selection -- orders and possible positions. This is what I meant. The first selection can be any of the items which means we can select it from any of the five positions.

The second selection can only be made from 4 items because we have taken one item already. The third selection can only be made from 3 items since we have taken two items already, on and on until the end of the operation.

Let's convert the factorial operation to code first and we will explain how to use it in the real world later in this article.

## Computing factorial with maths notation

```
n! =n * (n-1) * ... * 1
```

## Computing factorial with JavaScript

Now, let's compute factorial with iteration -- for or while loop.

### factorial using for-loop

```
function factorial(number) {
let result = 1;
if( number < 1) {
return result;
}
for(let position = number; position > 1; position--) {
result = result * position;//
}
console.log(result)
return result;
}
factorial(7)
```

Let's explain the code above step by step.

#### Step 1: create the result storage and the stopping point

```
let result = 1;
if( number < 1) {
return result;
}
```

#### Step 2: Multiply the number and its subsequent numbers repetitively

```
// does n = n * (n - 1).....* 1
for(let position = number; position > 1; position--) {
result = result * position;
}
```

#### Step 3: Return the result once the iteration is successful

```
console.log(result)
return result;
```

Bravo! We are done with factorial using for-loop; let's move on to factorial with while-loop.

### computing factorial using while loop

Using the while loop is very similar to for loop because we are just going to use an increment and a condition.

```
function factorial(number) {
let result = 1;
if( number < 1) {
return result;
}
while( number > 1) {
result = result * number;
number--
}
console.log(result)
return result;
}
factorial(7)
```

Let's explain the code step by step.

#### Step 1: Create the result storage, position and the stopping point

```
let result = 1;
if( number < 1) {
return result;
}
```

#### Step 2: Multiply the number and its subsequent numbers repetitively once the number is great than 1.

```
// does n = n * (n - 1).....* 1
while( number > 1) {
result = result * number;
number--
}
```

#### Step 3: Return the result once the iteration is successful

```
console.log(result)
return result;
```

Bravo! We are done with factorial using iteration; let's move on to factorial with a Recursion.

#### Pros of using iteration to compute factorial:

Compared to the recursive approach, it takes less memory because it doesn't store function stack.

#### Cons of using iteration to compute factorial:

It tends to lead to lengthier code compared to the recursive approach.

## Computing Factorial using Recursion

In this part of the article, we are going to be using Recursion to compute the factorial of numbers. So, you can watch the tutorial on Recursion below to have a better understanding of how recursion works.

Click the image below to watch it:

```
function factorial(number) {
if(number < 2) {
return 1;
}
// does n = n * (n - 1).....* 1
return number * factorial(number - 1)
}
const fiveFactorial = factorial(5);
console.log(fiveFactorial);
```

#### Step 1: Create the stopping point

```
if(number < 2) {
return 1;
}
```

#### Step 2: Multiply the number and its subsequent numbers repetitively.

```
// does n = n * (n - 1).....* 1
return number * factorial(number - 1)
```

This is the part where the function calls itself to multiply the number and its subsequent numbers. Below is how to think about it.

Iteration | function call | Interpretation |

1st | factorial (5) | 5 * factorial( 5 - 1 ) |

2nd | factorial (4) | 4 * factorial (4 - 1) |

3rd | factorial (3) | 3 * factorial (3 - 1) |

4th | factorial (2) | 2 * factorial (2 - 1) |

5th | factorial (1) | 1 * factorial (1 - 1) = 1 |

Total | Nothing to call | `5 * 4 * 3 * 2 * 1` = 120 |

The first call returns `5 * factorial (4)`

which means `factorial(4)`

will also return `4 * factorial(3)`

and it will go on and on until the number passed to the factorial function is less than 2 and then, it will return 1 to terminate the operation. That is how recursion works.

#### Pros of using recursion to compute factorial:

It is short, clean and easy to read.

#### Cons of using recursion to compute factorial:

It consumes more memory than the iterative approach and it has to account for function call stacks until the end of the operation.

## Using factorial in the real world

ID | amount | ...... |

1 | $9000 | ........... |

2 | $100,000 | ........... |

3 | $30,000 | ........... |

4 | $4,000 | ........... |

... | ..... | ........... |

100,000,000 | $30 | ........... |

Let's explore an example of factorial in action. If we have a table with about 100 million rows and we need to rank the users base on the biggest sales they made. Notice that we are not given any ranking factor and we have to determine from the table.

This means the biggest sales can be anywhere on the table and we are uncertain about the ranking factor because it is not given. Now, factorial comes to our rescue.

Factorial teaches us to start the selection from the maximum down to the minimum; we are going to use that here. So, we use SQL to query the max sales and then select the next 10 sales less than it while the selection is ordered with the max sale in descending order. Bravo!

Did you say how is that using factorial?

It uses a factorial selection approach but in this case, every sale can only occupy a position in our ranking. This is what I mean. When we select the max sale and put it in our ranking, it only occupies the first position, so it is 1 factorial (1!). The next to it can only occupy the second position and so on until we pick all the 10 sales.

In short, we start our selection from the maximum but the factorial of each selection is 1 factorial (1!).

## Conclusion

Understanding factorial in programming is very useful in handling uncertain situations just like the one we attempted in this article and this article is specifically written to give you an understanding of how to use factorials in the real world. Keep growing and don't forget, "a true Roman never surrenders". See you soon!