How to compute the average of an array after mapping each element to a value in JavaScript ?
Last Updated :
03 Jun, 2024
Improve
Given an array, the task is to compute the average of an array after mapping each element to a value.
Input : arr=[2, 3, 5, 6, 7]
Output: 4.6
Explanation : (2+3+5+6+7)/5 = 4.6
Input : [5,7,2,7,8,3,9,3]
Output: 5.5
Explanation : (5+7+2+7+8+3+9+3)/8 = 5.5
Here are some common approaches:
Table of Content
Using foreach() loop
- Iterate the array elements using foreach() loop.
- Store the sum of each element in a variable.
- The average of all elements will be sum/length where length is the size of the given array.
Example:
let arr = [2, 3, 5, 6, 7];
// Function to calculate the average of numbers
function avg(arr) {
let sum = 0;
// Iterate the elements of the array
arr.forEach(function (item, idx) {
sum += item;
});
// Returning the average of the numbers
return sum / arr.length;
}
console.log(avg(arr));
16
1
let arr = [2, 3, 5, 6, 7];
2
3
// Function to calculate the average of numbers
4
function avg(arr) {
5
let sum = 0;
6
7
// Iterate the elements of the array
8
arr.forEach(function (item, idx) {
9
sum += item;
10
});
11
12
// Returning the average of the numbers
13
return sum / arr.length;
14
}
15
16
console.log(avg(arr));
Output
4.6
Using for loop with ParseInt() method
- Iterate the numbers of the array using for loop
- Use ParseInt() function to parse the numbers in decimal format.
- Store the sum of numbers in a variable.
- The average of all elements will be sum/length where length is the size of the given array.
Example:
arr = [2, 3, 5, 6, 7];
var sum = 0;
// Iterating the elements of the loop
for (var i = 0; i < arr.length; i++) {
// Store the sum of all numbers
sum += parseInt(arr[i], 10);
}
// Taking the average
var avg = sum / arr.length;
console.log(avg);
14
1
arr = [2, 3, 5, 6, 7];
2
var sum = 0;
3
4
// Iterating the elements of the loop
5
for (var i = 0; i < arr.length; i++) {
6
7
// Store the sum of all numbers
8
sum += parseInt(arr[i], 10);
9
}
10
11
// Taking the average
12
var avg = sum / arr.length;
13
14
console.log(avg);
Output
4.6
Output:
4.6
Using reduce() function
- In this approach, We reduce i.e replace two numbers of the array with their sum in the original array.
- reduce() function returns a single value i.e the sum of all the numbers of the array.
- Store the return value in a variable (sum variable used).
- The average of all elements will be sum/length where length is the size of the given array.
Example:
var arr = [1, 2, 3, 4];
// Callback function calculating
// the sum of two numbers
function check(a, b) {
return a + b;
}
// Reducing the numbers of the array
var sum = arr.reduce(check);
// Calculating the average of the numbers
var avg = sum / arr.length;
console.log(avg);
14
1
var arr = [1, 2, 3, 4];
2
// Callback function calculating
3
// the sum of two numbers
4
function check(a, b) {
5
return a + b;
6
}
7
8
// Reducing the numbers of the array
9
var sum = arr.reduce(check);
10
11
// Calculating the average of the numbers
12
var avg = sum / arr.length;
13
14
console.log(avg);
Output
2.5
Using for…of Loop
In this approach, we iterate over each element in the array using the for…of loop to calculate the sum of the elements, and then compute the average.
- We initialize a variable sum to 0 to keep track of the sum of the array elements.
- The for…of loop is used to iterate over each element in the array. For each iteration, we add the current element’s value to the sum.
- After iterating through all elements, we calculate the average by dividing the sum by the length of the array.
Example:
// Define the array
let arr = [5, 7, 2, 7, 8, 3, 9, 3];
// Function to calculate the average using for...of loop
function avg(arr) {
let sum = 0;
// Iterate over the elements of the array
for (let value of arr) {
sum += value;
}
// Calculate the average
return sum / arr.length;
}
// Call the function and log the output
console.log(avg(arr));
18
1
// Define the array
2
let arr = [5, 7, 2, 7, 8, 3, 9, 3];
3
4
// Function to calculate the average using for...of loop
5
function avg(arr) {
6
let sum = 0;
7
8
// Iterate over the elements of the array
9
for (let value of arr) {
10
sum += value;
11
}
12
13
// Calculate the average
14
return sum / arr.length;
15
}
16
17
// Call the function and log the output
18
console.log(avg(arr));
Output
5.5