Home >Web Front-end >Front-end Q&A >How to write a javascript program to find the factorial of 9

How to write a javascript program to find the factorial of 9

WBOY
WBOYOriginal
2022-01-19 11:06:513402browse

Method: 1. Use the var statement to declare a variable with a value of 1; 2. Use a for loop with the "

How to write a javascript program to find the factorial of 9

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:

How to write a javascript program to find the factorial of 9

##Related recommendations:

javascript learning tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn