How to find the factorial of 10 in javascript

青灯夜游
Release: 2023-01-11 09:19:01
Original
5748 people have browsed it

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++) {
}
Copy after login

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;
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!