Created
June 21, 2016 14:01
-
-
Save brainyfarm/b855bce08dd6d1afac9c1b807efbf87f to your computer and use it in GitHub Desktop.
Olawale/FreeCodeCamp Algorithm: 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
/* | |
Here is my first solution without using a forloop | |
function factorialize_for_loop(num) { | |
// Define a variable factor and set an initial value of 1 | |
var factor = 1; | |
// A for loop from 1 to the given number | |
for(var n = 1; n < num+1; n++){ | |
// Continuous mulitplication | |
factor *= n; | |
} | |
// Return the final value | |
return factor; | |
} | |
*/ | |
// Here is the recursive version | |
function factorialize(num) { | |
return num > 0 ? num * factorialize(num - 1) : 1; | |
} | |
factorialize(3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment