Created
August 31, 2017 20:46
-
-
Save fed/a30119de53e719ad2e0e2f504cbf2c82 to your computer and use it in GitHub Desktop.
Factorial
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
// Recursively | |
// From `n`, all the way down to 1 | |
function factorial(n) { | |
if (n < 2) { | |
return 1; | |
} | |
return n * factorial(n - 1); | |
} | |
// Iteratively | |
// From 1, all the way up to `n` | |
function factorial(n) { | |
let result = 1; | |
for (let i = 1; i <= n; i++) { | |
result *= i; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment