Last active
August 29, 2015 14:13
-
-
Save epappas/8ff838e07d8562a1ac83 to your computer and use it in GitHub Desktop.
crazy JS factorial :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍