Skip to content

Instantly share code, notes, and snippets.

@Ajax30
Last active November 28, 2016 08:05
Show Gist options
  • Save Ajax30/15f4cb27fca91cea4be0182ba9d799ef to your computer and use it in GitHub Desktop.
Save Ajax30/15f4cb27fca91cea4be0182ba9d799ef to your computer and use it in GitHub Desktop.
Calculate the factorial of a number passed as the single parameter of a function
function fact() {
arr = [];
var n = arguments[0];
if (n == 0) {
arr.push(1);
} else {
for (i = 1; i <= n; i++) {
arr.push(i);
}
}
var result = arr.reduce(function(a, b) {
return a * b;
});
console.log(result);
}
fact(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment