Skip to content

Instantly share code, notes, and snippets.

@isaklafleur
Created October 9, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaklafleur/78dae66f7558ec6853edc550b87e07dc to your computer and use it in GitHub Desktop.
Save isaklafleur/78dae66f7558ec6853edc550b87e07dc to your computer and use it in GitHub Desktop.
An example of Recursion. A factorial function is defined as: factorial of n is n times the factorial of n - 1, except for factorial of 0 which is 1. Therefore, we can write this as:
function factorial(number) {
if (number === 0) { return 1; }
return number * factorial(number - 1);
}
// factorial(0) -> 1
// factorial(1) -> 1
// factorial(2) -> 2
// factorial(3) -> 6
// factorial(4) -> 24
// factorial(5) -> 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment