Skip to content

Instantly share code, notes, and snippets.

@ian128K
Created October 2, 2014 04:26
Show Gist options
  • Save ian128K/951f8d975a788dd8da58 to your computer and use it in GitHub Desktop.
Save ian128K/951f8d975a788dd8da58 to your computer and use it in GitHub Desktop.
Function for outputting the factorial of a number
function factorial(num) {
// If the number is less than 0, reject it.
if (num < 0) {
return "You can't do a factorial on negative numbers.";
}
// If the number is 0, its factorial is 1.
else if (num == 0) {
return 1;
}
var output = num;
while (num-- > 2) {
output *= num;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment