Method: 1. Use the var statement to declare a variable with a value of 1; 2. Use a for loop with the "
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
How to write a javascript program to calculate the factorial of 9
To write a JavaScript program to calculate the factorial of 9, you can use a for loop.
It is necessary to traverse the numbers 1~9, so the initial condition of the for loop can be set to i = 1, and the restriction condition can be i
Then in the loop body "{}", multiply the i values of each loop.
The example is as follows:
<html> <body> <p>点击按钮循求9的阶乘。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script> function myFunction() { var cj = 1; for (var i = 1; i <= 9; i++) { cj *= i; } document.getElementById("demo").innerHTML = cj; } </script> </body> </html>
Output result:
##Related recommendations:The above is the detailed content of How to write a javascript program to find the factorial of 9. For more information, please follow other related articles on the PHP Chinese website!