factorial of a number

前端 未结 28 2418
滥情空心
滥情空心 2020-12-09 11:57

I have the following code but it is not giving perfect result for factorial can u find it out plz



        
28条回答
  •  感情败类
    2020-12-09 12:43

    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.

提交回复
热议问题