Last active
January 6, 2018 18:26
-
-
Save frankfaustino/e528833dccf06d17bc83acbec852757b to your computer and use it in GitHub Desktop.
Factorial Calculator (Iterative vs 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
// The for loop performs much faster | |
function nFactorial (num) { | |
let factorial = 1; | |
for (let i = 1; i <= num; i++) { | |
factorial *= i; | |
} | |
return factorial; | |
} | |
// Recursion is about 11× slower | |
const nFactorial = n => n < 0 ? -1 : n === 0 ? 1 : n * nFactorial(n - 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment