Skip to content

Instantly share code, notes, and snippets.

@frankstepanski
Last active January 30, 2021 02:32
Show Gist options
  • Save frankstepanski/942d8561d0ef13486b0036169fcce763 to your computer and use it in GitHub Desktop.
Save frankstepanski/942d8561d0ef13486b0036169fcce763 to your computer and use it in GitHub Desktop.
Factorial algo using recursion
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