I am want to have function to computer factorial of integer numbers. I created function with loop under it and then call the function to pass the number and get the result. But it outcomes undefined, although all variables are declared properly.
<script>
var userInput;
var num;
var i =1;
var fact;
function myFactor (num){
fact = num * i;
for (i; i <= num; i++) {
fact = fact * i;
return fact;
}
}
var result = myFactor(fact);
userInput = prompt("Enter Value:","");
num = parseInt (userInput);
document.write(result);
</script>
There are many codes that achieve this but I want to learn that why my code does not work.