Last active
January 30, 2021 02:32
-
-
Save frankstepanski/942d8561d0ef13486b0036169fcce763 to your computer and use it in GitHub Desktop.
Factorial algo using recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function factorial(num, product = 1) { | |
// base case: num === 1; return product | |
// 1! = 1 || 0! = 1 | |
if (num <= 1) return product | |
// recursive case: factorial (num -1, num * product) | |
return factorial(num - 1, num * product); | |
} | |
console.log(factorial(4)); // -> 24 | |
console.log(factorial(6)); // -> 720 | |
console.log(factorial(0)); // -> 1 | |
// reference: https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/recursive-factorial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment