Forked from anonymous/bonfire-factorialize-a-number.js
Last active
December 7, 2015 23:27
-
-
Save jiyoon-koo/94b303b29cff1a7dfa9a to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/thetinybeaker 's solution for Bonfire: Factorialize a Number
This file contains hidden or 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
// Bonfire: Factorialize a Number | |
// Author: @thetinybeaker | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-factorialize-a-number | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
// Return factorial of a given interger passed though the factorialize function. | |
function factorialize(num) { | |
// if num (recursively gotten to value of 0, or starts out at 0) is 0, then the function returns 1. | |
if (num === 0){ | |
return 1; | |
} | |
// if num (starts out) at below 0, function returns 1. num can never recursively arrive at below 0, since that possibility ends with num === 0 returning 1. | |
else if (num < 0){ | |
return -1; | |
} | |
// call stack. number is factorialized by calling the same function over and over again, with the new num value 1 smaller than previous num value. | |
else { | |
return (num * factorialize(num-1)); | |
} | |
} | |
factorialize(5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment