Factorial Algorithm: ES5 & ES6 JavaScript Solutions

Factorial Algorithm: ES5 & ES6 JavaScript Solutions

The factorial algorithm is one of the common whiteboard algorithms that you can come across.

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 integervalue. 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 integerargument 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 loop: 3 == true  | 2nd loop: 2 == true | 3rd loop: 1 == true | 4th 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).


By : Jan Sysala


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.

How to Retrieve full Profile of LinkedIn User using Javascript

I am trying to retrieve the full profile (especially job history and educational qualifications) of a linkedin user via the Javascript (Fetch LinkedIn Data Using JavaScript)

Java vs. JavaScript: Know The Difference

Java vs. JavaScript: Know the Difference, Java vs. JavaScript: What's the Difference? Java vs. JavaScript: Major Similarities and Differences. pros and cons of JavaScript and Java.