Calculate a factorial Javascript code example
Example 1: javascript factorial
function factorial(n) {
if (n < 0) return;
if (n < 2) return 1;
return n * factorial(n - 1);
}
Example 2: javascript factorial stack
function factorial(n) {
let s = new Stack();
while (n>1){
s.push(n--);
}
let product = 1;
while (s.length() > 0) {
product *= s.pop();
}
return product;
}