Created
October 2, 2014 04:26
-
-
Save ian128K/951f8d975a788dd8da58 to your computer and use it in GitHub Desktop.
Function for outputting the factorial of a number
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
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