Home > Web Front-end > JS Tutorial > body text

What is the code for finding the average in JavaScript?

WBOY
Release: 2021-11-23 15:45:24
Original
9232 people have browsed it

In JavaScript, you can use for loops, "/" and "=" operators to find the average. The implementation code is "for(var i=0;i

What is the code for finding the average in JavaScript?

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

What is the code for finding the average in JavaScript

1. Create a new html file and name it test.html, which is used to explain the inside of js How to find the average. Create an array of numbers used to calculate the average. Then use the length attribute to find the length of the array, that is, the number of array elements.

What is the code for finding the average in JavaScript?

2. In the test.html file, initialize a total variable to save the cumulative value of the number. Through the for loop, iterate through each number in the array and use "=" to accumulate the numbers.

In the test.html file, divide the accumulated result by the total number of numbers to get the average value, and use the alert() method to output the result.

What is the code for finding the average in JavaScript?

Open the test.html file in the browser, click the button to view the results.

What is the code for finding the average in JavaScript?

Summary:

1. Create an array of numbers.

2. Find the length of the array through the length attribute.

3. Through the for loop, traverse each number in the array, and use "=" to accumulate the numbers.

4. Divide the cumulative result by the total number of numbers to get the average value.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What is the code for finding the average 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