Skip to content

Instantly share code, notes, and snippets.

@lucian303
Last active August 29, 2015 14:16
Show Gist options
  • Save lucian303/5efa7adde757dc6089c3 to your computer and use it in GitHub Desktop.
Save lucian303/5efa7adde757dc6089c3 to your computer and use it in GitHub Desktop.
Factorial
// Factorial
// Reduce
var number = 7;
arr = [];
for (var x = 1; x <= number; x++) {
arr.push(x);
}
alert(arr.reduce(function (a, x) {
return a * x;
}));
// Recursive
function fact(x) {
if (x === 1) {
return 1;
}
return x * fact(x - 1);
}
alert(fact(number));
// Loop
var factorial = 1;
for (var x = 1; x <= number; x++) {
factorial *= x;
}
alert(factorial);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment