Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active December 27, 2015 19:16
Show Gist options
  • Save mmloveaa/9fdd3a65e4b1a140c891 to your computer and use it in GitHub Desktop.
Save mmloveaa/9fdd3a65e4b1a140c891 to your computer and use it in GitHub Desktop.
Factorial using recursion
// 12/21/2015
//This program runs well but we need to make it more elegant.
// Try to rewrite the code of the function factorial in way to use recursion.
// You can not use looping(for, while, forEach).
// Return examples
// factorial(3)=>3 x 2 x 1 = 6
// factorial(2)=>2 x 1 = 2
// factorial(0)=>1 = 1
// My Solution:
function factorial(num){
// var total=0;
if(num===0) {
return 1;
}
else {
return num*factorial(num-1);
}
// return total;
}
factorial(5);
//54x3x2x1=120
// function factorial(num) {
// return num > 1 ? num * factorial(num - 1) : 1
// }
// function factorial(num){
// var total=0;
// if(num===0) {
// return 1;
// }
// else {
// total=num*factorial(num-1);
// }
// return total;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment