Skip to content

Instantly share code, notes, and snippets.

@brainyfarm
Created June 21, 2016 14:01
Show Gist options
  • Save brainyfarm/b855bce08dd6d1afac9c1b807efbf87f to your computer and use it in GitHub Desktop.
Save brainyfarm/b855bce08dd6d1afac9c1b807efbf87f to your computer and use it in GitHub Desktop.
Olawale/FreeCodeCamp Algorithm: Factorialize a Number
/*
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