Home  >  Article  >  Web Front-end  >  How to find the average of an array in jquery

How to find the average of an array in jquery

WBOY
WBOYOriginal
2022-04-18 15:16:372361browse

Method: 1. Use "var sum=0" to declare the sum value to be 0; 2. Use "for(var i=0;i

How to find the average of an array in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

Jquery finds the average of an array

In jquery, by accumulating the array elements and dividing by the length of the array, the average of the array can be obtained.

Create a new html file, named test.html, to explain how jquery calculates the average of an array. Within the js tag, create an array and initialize a variable $sum.

Use a for loop to traverse the array to accumulate array values. Get the length of the array through the length attribute, and then divide the sum of the array values ​​by the length to get the average of the array. Use the write() method to output the calculated results.

How to find the average of an array in jquery

Open the test.html file in the browser to view the results.

How to find the average of an array in jquery

Summary:

1. Create an array and initialize a variable $sum.

2. Use a for loop to traverse the array to achieve accumulation of array values.

3. Get the length of the array through the length attribute, and then divide the sum of the array values ​​by the length to get the average value of the array.

4. Use the write() method to output the calculated results.

5. Open the test.html file in the browser and view the results.

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to find the average of an array in jquery. 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