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


javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Data Types In JavaScript

JavaScript data types are kept easy. While JavaScript data types are mostly similar to other programming languages; some of its data types can be unique. Here, we’ll outline the data types of JavaScript.

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.

Create a Line Through Effect with JavaScript

In this post we are going to create an amazing line through effect, with help of CSS and lots of JavaScript. So, head over to your terminal and create a folder LineThroughEffect. Create three files -index.html, main.js and styles.css inside it.

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.