Created
May 19, 2014 14:12
-
-
Save z-------------/11508eaade55da7232be to your computer and use it in GitHub Desktop.
Two ways to calculate factorials: recursively and iteratively. http://jsperf.com/recursive-vs-iterative-factorial
This file contains hidden or 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
// recursive | |
var factorial = function(n) { | |
if (n == 1) { | |
return 1; | |
} else { | |
return n * factorial(n-1); | |
} | |
} | |
// iterative | |
var factorial = function(n) { | |
for (i=n-1;i>=1;i--) { | |
n*=i; | |
} | |
return n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment