Factorial

Home page

A factorial is the product of an integer and all the intergers below it; e.g. factorial four (4!) is equal to 24.

4! = 4 * 3 * 2 * 1 = 24

Try out my solution below.
Answer:

Iterative:
factorial(n) {
    let res = 0;
    if (isNaN(n) || n < 0)
        res = 'Please only use a positive integers, which must be greater than or equal to zero.'
    else if (n > -1 && n < 2) res = 1;
    else {
        for (var i=n; i>1; i--)
            n *= (i-1);
        res = n;
    }
    return res;
}
Recursion:
factorial = (n) => {
    let res;
    if (isNaN(n) || n < 0)
      res = 'Please only use a positive integers, which must be greater than or equal to zero.'
    else if (n > 0) res = n * factorial(n-1);
    else return 1;
    return res;
  };
}