Skip to content

Instantly share code, notes, and snippets.

@epappas
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epappas/8ff838e07d8562a1ac83 to your computer and use it in GitHub Desktop.
Save epappas/8ff838e07d8562a1ac83 to your computer and use it in GitHub Desktop.
crazy JS factorial :)
var N = 10;
(function(N){
var arr = [];
for(var i=N+1; i--;) arr.push(i);
arr.pop(); // last elem is 0
return arr;
})(N).reduce(function (n, i) {
return n * i;
}, 1);
function fact(n, cb) {
if (n < 1) return cb(null, 1);
setImmediate(fact.bind(this, n-1, function(err, nn) {
cb(null, n * nn);
}));
}
@flentini
Copy link

flentini commented Jan 7, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment