Files
grokking_algorithms/03_recursion/javascript/03_factorial.js
Yulia Kolupaeva 0c4fdddb12 factorial formula adjustement
The formula in book is incorrect because it doesn't include the factorial(0) case and leads to infinite recursion in this case.
2021-02-16 18:10:32 +03:00

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));