I have the following code but it is not giving perfect result for factorial can u find it out plz
var factorialNumber , factorial=1;
factorialNumber=prompt("Factorial Number" , "write Factorial Number");
for(var i = 1; i<= factorialNumber;i++){
factorial *= i;
}
alert(factorial);
The code above first defines two variables, factorialNumber
and factorial
. factorial
is initialized with 1. factorialNumber
will get the result of the prompt
(a number is expected) and then, using a cycle, in each step, factorial is multiplied with the index of the step, which is represented by i
. When successfully calculated, we show the result using alert
.