Created
June 20, 2018 10:20
-
-
Save elitenomad/bef5a58a6bb9b9c963a55be17f0230df to your computer and use it in GitHub Desktop.
Factorial (Recursive and Regular loop methods)
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
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