The formula in book is incorrect because it doesn't include the factorial(0) case and leads to infinite recursion in this case.
12 lines
199 B
JavaScript
12 lines
199 B
JavaScript
/**
|
|
* Consider the factorial of the number
|
|
* @param {number} x Number
|
|
* @returns {number} Result
|
|
*/
|
|
function fact(x) {
|
|
if (x === 0) return 1;
|
|
return x * fact(x - 1);
|
|
}
|
|
|
|
console.log(fact(5));
|