RosettaCodeData/Task/Factorial/JavaScript/factorial-1.js

13 lines
247 B
JavaScript
Raw Permalink Normal View History

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"; }
2020-02-17 23:21:07 -08:00
var result = 1;
2015-11-18 06:14:39 +00:00
//we skip zero and one since both are 1 and are identity
while (n > 1) {
2020-02-17 23:21:07 -08:00
result *= n;
2017-09-23 10:01:46 +02:00
n--;
2015-11-18 06:14:39 +00:00
}
2020-02-17 23:21:07 -08:00
return result;
2013-04-10 21:29:02 -07:00
}