Factorial Algorithm: ES5 & ES6 JavaScript Solutions

Jan Sysala on January 27, 2019

The factorial algorithm is one of the common whiteboard algorithms that you can come across. Google's definition of factorial: The product of... [Read Full]
 

I like when my code is executed vertically.
Probably too verbose but I like the "clean" feel to it.

const factorial = (x) => {
  if (x === 0){
    return 1;
  } else if(x === 1) {
    return x;
  } else {
    return x * factorial(x - 1) 
  }
}
 

Nice explenation on factorial. There is just tiny problem.

Every time I see

if(i)

I cringe a bit.
Please stop this and write

if(i > 0)

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?

function factorial(integer) {
   return integer ? (integer * factorial(integer - 1)) : 1 
}
 

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);

code of conduct - report abuse