Exercise: Iterative Factorial
Exercise prerequisites:
- JavaScript Functions
- JavaScript Control Flow
- 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:
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)
factorial(4)
factorial(5)
factorial(10)
factorial(0)
factorial('')
factorial(true)
First, let's decide on what exactly do we need to implement the factorial()
function:
- We need to have a parameter that represents the number whose factorial to compute. Let's call it
n
. - 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. - We also need to lay out an
if
conditional to check for the case whenn
is0
(i.e ::0!::) and return1
in that case. - Finally, we need a
for
loop to iterate as many times as the integern
, and thus compute the factorial ofn
.
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;
}
Spread the word
Think that the content was awesome? Share it with your friends!
Join the community
Can't understand something related to the content? Get help from the community.