Skip to content

Instantly share code, notes, and snippets.

@haase1020
Created December 6, 2021 09:46
Show Gist options
  • Save haase1020/5e53f1c701797e29a454b39d914700e2 to your computer and use it in GitHub Desktop.
Save haase1020/5e53f1c701797e29a454b39d914700e2 to your computer and use it in GitHub Desktop.
factorial iterative
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