- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript: Computing the average of an array after mapping each element to a value
In this article, we are going to fetch all the values from an array and after mapping each element to a numerical value we will calculate its sum thus to compute its average.
Given below is an array consisting of values. We will compute the average by summing up the values and then dividing it by the number of values available.
Input
[1, 2, 3, 4, 5]
Output
3
Explanation
(1+2+3+4+5)/5 = 3
Input
[2, 4, 7, 9, 1, 3, 8]
Output
4.85
Explanation
(2+4+7+9+1+3+8)/7 = 4.85
Approach #1
We will iterate over the list of elements using the forEach() loop.
On iterating each element, we will convert it to a value and add it to a sum.
Once we get the sum of all numbers, we will divide it by the total numbers.
Example 1
In the below example, we are computing the average of an array after finding the sum of all the values.
# index.html
<!DOCTYPE html> <html> <head> <title>Computing the Average</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> arr = [1, 2, 3, 4, 5]; // Computing the average function avg(arr) { var sum = 0; // Iterate over the elements of the array arr.forEach(function (item, idx) { //Adding the elements to the sum sum += item; }); // Returning the average of the numbers return sum / arr.length; } console.log("Average of all the numbers is: "+avg(arr)); </script> </body> </html>
Output
On successful execution of the above program, the browser will display the following result −
Welcome To Tutorials Point
And in the console, you can find the resul −
Average of all the numbers is: 3
Approach #2
Another approach is to use the parseInt() method for mapping the values to a number.
We will iterate over the list of elements using the for() loop.
Use parseInt() to parse the numbers in the desired decimal type
Store the sum of numbers in a variable.
Find the sum of all the numbers by using the below formula
$$average\:=\:{sum}/{length\:of\:numbers}$$
Example 2
In the below example, we are computing the average of an array after finding the sum of all the values. We apply to parseInt() to find the sum of all values.
<!DOCTYPE html> <html> <head> <title>Computing the Average</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> arr = [2, 4, 7, 9, 1, 3, 8]; var sum = 0; // Iterating the elements by using the for loop for (var i = 0; i < arr.length; i++) { // Store the sum of all numbers in a variable sum += parseInt(arr[i], 10); } // Calculating the Average var avg = sum / arr.length; console.log("Average is: " + avg); </script> </body> </html>
Output
On successful execution of the above program, the browser will display the following result-
Welcome To Tutorials Point
And in the console, you can find the result −
Average is: 4.857142857142857