Skip to content

Commit

Permalink
Revert "Revert "optimize""
Browse files Browse the repository at this point in the history
  • Loading branch information
contra committed Jun 24, 2017
1 parent 6143479 commit a72ac15
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions index.js
@@ -1,11 +1,6 @@
module.exports = function(n) {
if (n < 0) {
throw new RangeError("Cannot take factorial out of a negative number.");
} else {
return factorial(n);
}
}

function factorial(n) {
return n === 0 ? 1 : n * factorial(n - 1);
module.exports = function factorial(n) {
if (n === 0) return 1
if (n < 0) return NaN
for (var r = 1; n > 1; n--) r *= n
return r
}

0 comments on commit a72ac15

Please sign in to comment.