Skip to content

Instantly share code, notes, and snippets.

@z-------------
Created May 19, 2014 14:12
Show Gist options
  • Save z-------------/11508eaade55da7232be to your computer and use it in GitHub Desktop.
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
// 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