Home > Article > Web Front-end > How to find the factorial of 10 in javascript
Method to find 10 factorial: 1. Use the "for (var i=1;i
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:
##[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!