Home  >  Article  >  Web Front-end  >  How to find the factorial of 10 in javascript

How to find the factorial of 10 in javascript

青灯夜游
青灯夜游Original
2021-09-09 16:24:215776browse

Method to find 10 factorial: 1. Use the "for (var i=1;i

How to find the factorial of 10 in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The factorial of 10 is: 1*2*3*4*5*6*7*8*9*10

The result is: 3628800

If we want to use javascript to find 10 factorials, we can use a for loop to achieve it.

If you want to find the factorial of 10, you need to traverse the numbers from 1 to 10. Therefore, the initial condition of the for loop can be set to i = 1, and the restriction condition can be i

for (var i = 1; i <= 10; i++) {
}

Then in the loop body "{}", multiply the i values ​​of each loop. This requires an intermediate quantity cj to store the product. The initial value of the variable cj must be 1, so as not to affect the result. There are two ways to write it (just choose one):

cj *= i;
//或
cj = cj * i;

After the loop ends, the value of variable cj will be the factorial of 10, and then output it.

The complete implementation code is given below:

var cj = 1;
for (var i = 1; i <= 10; i++) {
    cj *= i;
}
console.log( "10的阶乘为: " + cj);

Output results:

How to find the factorial of 10 in javascript

##[Recommended learning:

javascript advanced tutorial

The above is the detailed content of How to find the factorial of 10 in javascript. 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