How to compute the average of an array after mapping each element to a value in JavaScript ? Last Updated : 03 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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() loopUsing for loop with ParseInt() methodUsing reduce() functionUsing for...of LoopUsing foreach() loopIterate 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: JavaScript 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)); Output4.6 Using for loop with ParseInt() methodIterate the numbers of the array using for loopUse 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: JavaScript 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); Output4.6 Output: 4.6Using reduce() functionIn 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: JavaScript 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); Output2.5 Using for...of LoopIn 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: JavaScript // 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)); Output5.5 Comment More infoAdvertise with us Next Article How to compute the average of an array after mapping each element to a value in JavaScript ? rohit2sahu Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads How to Compute the Sum and Average of Elements in an Array in JavaScript? Computing the sum and average of elements in an array involves iterating through the array, accumulating the sum of its elements, and then dividing the sum by the total number of elements to get the average. This can be achieved using various techniques, from basic loops to more advanced functional 3 min read Max/Min value of an attribute in an array of objects in JavaScript In this article, we are given an array of objects and the task is to get the maximum and minimum values from the array of objects. For this approach, we have a few methods that will be discussed below. Methods to get Min/Max values:using JavaScript apply() and Array map()using JavaScript Array reduc 3 min read How to create an array of elements, ungrouping the elements in an array produced by zip in JavaScript ? We can create an array of elements by ungrouping the elements in an array produced by zip by using different methods in JavaScript such as Math.max(), array specific methods namely Array.prototype.map(), Array.prototype.reduce() and Array.prototype.forEach(). This operation can also be termed as unz 2 min read How to calculate the XOR of array elements using JavaScript ? In this article, we are given an array, and the task is to find the XOR of Array elements using JavaScript. There are two approaches to solve this problem which are discussed below: Method 1: It uses a simple method to access the array elements by an index number and use the loop to find the XOR of 2 min read How to Convert an Array of Objects to Map in JavaScript? Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon 3 min read How to Find the Length of an Array in JavaScript ? JavaScript provides us with several approaches to count the elements within an array. The length of an array lets the developer know whether an element is present in an array or not which helps to manipulate or iterate through each element of the array to perform some operation. Table of Content Usi 3 min read How to get median of an array of numbers in JavaScript ? In this article, we will see how to find the median of an array using JavaScript. A median is a middle value in a given set of numbers or data. Examples: Input arr1 : [1 4 7 9]Output : 5.5Explanation : The median of the two sorted array is the middle elements which is 5 if the arrayLength =arr1.leng 3 min read How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati 4 min read How to find an average color of an image using JavaScript ? The average color of an image can be found with JavaScript by drawing the image on a canvas element. The following steps have to be performed to get the average color of an image: 1. The image is first drawn on the canvas using the method context.drawImage() of the Canvas 2D API. This method takes i 3 min read Implement polyfill for Array.prototype.map() method in JavaScript In this article, we will learn how to implement a polyfill for an Array.prototype.map() method in JavaScript. What is a polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser y 4 min read Like