2013-04-10 21:29:02 -07:00
|
|
|
function factorial(n) {
|
2015-11-18 06:14:39 +00:00
|
|
|
//check our edge case
|
|
|
|
|
if (n < 0) { throw "Number must be non-negative"; }
|
|
|
|
|
|
|
|
|
|
var sum = 1;
|
|
|
|
|
//we skip zero and one since both are 1 and are identity
|
|
|
|
|
while (n > 1) {
|
|
|
|
|
sum *= n;
|
2017-09-23 10:01:46 +02:00
|
|
|
n--;
|
2015-11-18 06:14:39 +00:00
|
|
|
}
|
|
|
|
|
return sum;
|
2013-04-10 21:29:02 -07:00
|
|
|
}
|