Skip to content

Instantly share code, notes, and snippets.

@fed
Created August 31, 2017 20:46
Show Gist options
  • Save fed/a30119de53e719ad2e0e2f504cbf2c82 to your computer and use it in GitHub Desktop.
Save fed/a30119de53e719ad2e0e2f504cbf2c82 to your computer and use it in GitHub Desktop.
Factorial
// 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