Objective

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

Difficulty

Easy

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