Course: JavaScript

Progress (0%)

# Exercise: Iterative Factorial

Exercise 5 Easy

Prerequisites for the exercise

1. JavaScript Functions
2. JavaScript Control Flow
3. All previous chapters

## Objective

Create a factorial() function to compute the factorial of a given integer.

## Description

The factorial of an integer $n$ is the product of all integers starting from that integer all the way down to $1$.

In terms of a function, it could be expressed as follows:

$f(n) = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1$

For instance:

$1! = 1$
$2! = 2 \times 1 = 2$
$3! = 3 \times 2 \times 1 = 6$
$4! = 4 \times 3 \times 2 \times 1 = 24$
$5! = 5 \times 4 \times 3 \times 2 \times 1 = 120$

$0!$ is a special case — it evaluates to $1$.

In this exercise, you have to create a function factorial() that takes in an integer and returns its factorial.

The function MUST compute the factorial iteratively i.e. using a for (or maybe while) loop. In addition to this, if the given argument to factorial() is not a number, the function should return null.

Shown below are a couple of examples of the function's usage:

factorial(3)
6
factorial(4)
24
factorial(5)
120
factorial(10)
3628800
factorial(0)
1
factorial('')
null
factorial(true)
null

## New file

Inside the directory you created for this course on JavaScript, create a new folder called Exercise-5-Iterative-Factorial and put the .html solution files for this exercise within it.

## Solution

First, let's decide on what exactly do we need to implement the factorial() function:

1. We need to have a parameter that represents the number whose factorial to compute. Let's call it n.
2. Secondly, we need to check if the provided argument is not a number. If it isn't, we return null, as instructed in the description of the exercise above.
3. We also need to lay out an if conditional to check for the case when n is 0 (i.e $0!$) and return 1 in that case.
4. Finally, we need a for loop to iterate as many times as the integer n, and thus compute the factorial of n.

And this is all that we need.

Below shown is the definition of factorial():

function factorial(n) {
if (typeof n !== 'number') {
return null;
}
if (n === 0) {
return 1;
}

var result = 1;
for (var i = 2; i <= n; i++) {
result = result * i;
}
return result;
}

First, we check if the given argument is not a number via the conditional in line 2. If it isn't a number, null is returned. This, as we know, effectively ends the function's execution as well.

Moving on, the next check performed is of n being 0. If that's the case, 1 is returned.

Finally, if none of the cases above is met, we proceed with the actual factorial-computing logic. The variable result is meant to hold the factorial of the given number.

At each point in the iteration, we multiply the previous value stored in result with i. This is exactly the definition of a factorial — the product of numbers from $1$ upto that number (inclusive).

Note that the for loop begins at i = 2, not i = 1. The reason for beginning at i = 2 is because there is no point of beginning at i = 1. That is, any number multiplied by 1 would give that same number back; hence, even if we begin at i = 1, there would be no difference at all in the final result.

Although the code above works absolutely perfectly, there is one thing that could be slightly improved.

That is, if we notice it carefully, the check for n === 0 (in line 5) can be removed completely from the code. If n is 0, the for loop's condition i <= n (with i = 2) wouldn't be true and hence, its body won't execute anyway. Ultimately, result would be returned, giving us the value 1 — just as we want it to be.

So let's go on and update factorial():

function factorial(n) {
if (typeof n !== 'number') {
return null;
}

var result = 1;
for (var i = 2; i <= n; i++) {
result = result * i;
}
return result;
}

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage