Skip to content

Instantly share code, notes, and snippets.

@willeswa
Created June 8, 2018 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willeswa/69bdeb784c8b2fb9e315ed87397b41d9 to your computer and use it in GitHub Desktop.
Save willeswa/69bdeb784c8b2fb9e315ed87397b41d9 to your computer and use it in GitHub Desktop.
bKByov
var num = prompt("Give number");
function fact(num) {
var fact;
while(num>0){
fact *= num--;
num--;
}
return fact;
}
alert("Factorial is: "+fact(num));
@InfoJeff
Copy link

Line 5 needs to be
var fact = 1;
so you don't start by multiplying by 0

Line 8 needs to be
fact *= num;
so you are multiplying "fact" by the current counter BEFORE you decrement "num" by 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment