# Factorial Algorithm: ES5 & ES6 JavaScript Solutions

###
Jan Sysala
*Updated on *
・3 min read

The factorial algorithm is one of the common `whiteboard`

algorithms that you can come across.

Google's definition of factorial:

```
The product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24.
```

That is:

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

## ES5: While method

```
function factorial(integer) {
final = 1
while (integer > 0) {
final *= integer
integer = integer - 1
}
return final
}
```

Let's break down the code:

```
function factorial(integer) {
final = 1
while (integer > 0) {
final *= integer
```

We start with assigning an initial value of `1`

to variable `final`

. Inside of the later used `while`

method we will be able to assign the same value as an `integer`

variable by multiplying `final`

variable by the `integer`

argument variable. By doing this we make sure we will start with multiplying the highest possible number by the next smallest possible number.

```
while (integer > 0) {
```

Then we run a `while`

method where the condition is that it will loop as long as the value of `integer`

is greater than zero.

```
final *= integer
integer = integer - 1
```

Inside of the `while`

loop we multiply the variable `final`

by the `integer`

value. The `integer`

variable has a `new value`

assigned in every loop execution.

```
integer = integer - 1
}
return final
```

At some point, the value of the `integer`

variable will be `0`

(because we deduct `1`

from the `integer`

value in every loop execution - `integer = integer - 1`

), because of that the `while`

loop will stop executing and the value of `final`

variable will be the factorial of the value of `integer`

argument variable we passed into the function.

Let's see an example with `integer`

variable having a value of `3`

.

```
integer = 3;
function factorial(integer) {
final = 1
while (integer > 0) {
final *= integer // 1st loop: 1 * 3 = 3 | 2nd loop: 3 * 2 = 6 | 3rd loop: 6 * 1 = 6
integer = integer - 1 // 1st loop: 3 - 1 = 2 | 2nd loop: 2 - 1 = 1 | 3rd loop: 1 - 1 = 0
}
return final // 6 because of the 3rd loop of final *= integer = 6 * 1
}
```

## ES6: Arrow Loop Function with Conditional (ternary) operator

```
const factorial = integer => integer ? (integer * factorial(integer - 1)) : 1;
```

Here we have a one-line arrow function that takes `integer`

as argument variable. Inside of the function is shorthand if else statement.

```
/*const factorial = integer =>*/ integer ? /*(integer * factorial(integer - 1)) */ : 1;
```

The condition of the `If Else`

statement is `if integer == true`

. That must be (in our case) any positive number. And by now we know that if the condition is `false`

it will return value of `1`

.

```
/* const factorial = integer => integer ?*/ (integer * factorial(integer - 1)) /* : 1; */
```

Here comes the `fun`

part. If the condition is evaluated as `true`

, we will multiply the `integer`

variable by the result of the `factorial`

function itself with the `integer`

value being deducted by `1`

(`integer - 1`

).

Now we see that `factorial`

function is a `loop function`

that calls itself as long as the value of `integer`

(that is being deducted by `1`

with every loop) is greater than `0`

.

```
/* const factorial = integer =>*/ integer ? (integer * /*factorial*/(integer - 1)) : 1;
```

Once the statement evaluation of the `integer ?`

becomes false (in our case `1 - 1 = 0`

) it will multiply the last assigned value of `integer`

by `1`

(`: 1`

) and stops the loop.

Let's see an example with `integer`

variable having a value of `3`

.

```
integer = 3;
const factorial = integer =>
integer ? // 1st execution: 3 == true | 1st loop: 2 == true | 2nd loop: 1 == true | 3rd loop: 0 == false
(integer * factorial(integer - 1)) // 1st loop: 3 * 2 | 2nd loop: 6 * 1
: 1; // 6 * 1 and stops the loop
factorial(integer); // 6
```

Hope you enjoyed this algorithm code breakdown and let me know if you have any other favourite solution (there's many off course). ❤️

I like when my code is executed vertically.

Probably too verbose but I like the "clean" feel to it.

Nice explenation on factorial. There is just tiny problem.

Every time I see

I cringe a bit.

Please stop this and write

We do not need to save bytes anymore and it is much more expressive. Also just checking whether

`integer`

is not 0 is error prone.It might seem like I'm being extra pedantic (I sure as hell am), but there is a reason. In the small examples this won't make a difference. A problem arises when this kind of behaviour slips in to real world code. There this can go undetected and cause errors. This is why I always try to be as expressive as possible, even in academic examples and coding dojos.

How is it different from following?

Hi Akash,

as I mentioned in the post, there are many solutions to the Factorial Algorithm.

I chose those two because I like how self-explanatory is the code in the solution with

`while method`

and I like the second one because it's a nice short one-liner.The one you are mentioning is the same as the second one in the post. The difference is that you are using function declaration/expression instead of an arrow function. With the ES6 arrow function, it's just shorter and a one-liner.

I like how this shows both ES5 and ES6 solutions, but since at the beginning of the article you mentioned it is common in white-boarding exercises, it might have been interesting or useful to also talk about how to improve the efficiency of the algorithms. Such as using a dictionary/hashmap to store previously computed values. Or perhaps you plan on doing that in the future?

I mention it, because I have actually gotten asked this question in one of my first white boarding interviews a few years ago. Initially, I wrote down something similar to the ES5 approach but in python. The interviewer then said "ok but what if there isn't an upper limit on your input, what if someone tried to find the factorial of a thousand or ten thousand, etc?" So I did have to make it more efficient using a dictionary. Sometimes interviewers are looking for this thought process and improvement.

Hi Jared,

thank you for the comment. We are still in the #beginner section here and I would like to breakdown the basic most common algorithms first.

However, thank you for the suggestion, I will take note and maybe do the breakdown of more advanced factorial solution later.

If you feel like solving more advanced factorial (in JS?) take a look at this HackerRank challenge.

Also, thank you for sharing the real-life experience, always good to know what can one expect on an interview.

`const factorial = [...Array(number).keys()].map(number => number + 1).reduce(acc, n => acc * n), 0);`