Created
October 9, 2017 21:02
-
-
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:
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(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