Last active
March 2, 2018 14:46
-
-
Save isabellachen/9cbb6e434b3762a96cd5ff37d5978db4 to your computer and use it in GitHub Desktop.
factorial function in JS two ways
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
function factorial (n) { | |
let product = 1 | |
for (let i=n; i>0; i--) { | |
product = product * i | |
} | |
return product | |
} | |
console.log(factorial(5)) | |
function factorialRecursion (n) { | |
if (n===0) { | |
return 1 | |
} else { | |
return n * factorialRecursion(n-1) | |
} | |
} | |
console.log(factorialRecursion(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment