Skip to content

Instantly share code, notes, and snippets.

@elitenomad
Created June 20, 2018 10:20
Show Gist options
  • Save elitenomad/bef5a58a6bb9b9c963a55be17f0230df to your computer and use it in GitHub Desktop.
Save elitenomad/bef5a58a6bb9b9c963a55be17f0230df to your computer and use it in GitHub Desktop.
Factorial (Recursive and Regular loop methods)
const factorialWithRecursive = (n) => {
if(n == 1){
return n;
}else{
return (n * factorialWithRecursive(n - 1));
}
}
const factorialRecursive = factorialWithRecursive(5);
console.log(`What is factorialRecursive ${factorialRecursive}`);
const factorialWithRegularLoop = (n) => {
let result = 1;
for(let i = n; i > 1 ; i--) {
result = result * i;
}
return result;
}
const factorialRegularLoop = factorialWithRegularLoop(5);
console.log(`What is factorialRegularLoop ${factorialRegularLoop}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment