Created
December 6, 2021 09:46
-
-
Save haase1020/5e53f1c701797e29a454b39d914700e2 to your computer and use it in GitHub Desktop.
factorial iterative
This file contains 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 answer = 1; | |
if (n == 0 || n == 1) { | |
return answer; | |
} else { | |
for (var i = n; i >= 1; i--) { | |
console.log("running"); // should log 5 times | |
answer = answer * i; | |
} | |
return answer; | |
} | |
} | |
console.log(factorial(5)); // 5*4*3*2*1 = 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment