Average or Arithmetic mean of an array using Javascript

The goal of this article is to calculate the average of an array using JavaScript. Before we do that, let’s quickly understand what the terms ‘Average’ & ‘Array’ mean.

Table of Contents

Average or Arithmetic mean is a representation of a set of numbers by a single number. Its value can be obtained by calculating the sum of all the values in a set and dividing the sum by the number of values.

For example:

  • Consider the following set of numbers: 1, 2, 3 & 4
  • Average/Mean = (1+2+3+4)/4

An array is a container object that holds a fixed number of values of a single type. An array’s length, once created, would remain constant/fixed.

You can go through other basic concepts of object oriented programming such as looping, conditional statements, user defined functions and classes to understand this blog better

Explanation

Simple approach to finding the average of an array

We would first count the total number of elements in an array followed by calculating the sum of these elements and then dividing the obtained sum by the total number of values to get the Average / Arithmetic mean

Breaking down the array average algorithm

Mean of an array can be obtained in 3 steps:

Step 1: Finding the total number of elements in an array (basically, its length)

This can be obtained by calculating the length of the array using the .length method.

Step 2: Finding the sum of all the elements of an array (sum)

We would need to traverse the array to find the sum. We initialize a variable called ‘total’ and loop over the array and add each element of the array to the ‘total’ variable

Step 3: Dividing the values obtained in Step 1 & 2.(sum/length)

Code

arry = [1,2,3,4];
var noofElements = arry.length;
var total = 0;</span><span id="337f" class="dh io ga ei ip b de it iu iv iw ix ir w is">for(var i = 0; i < arry.length; i++){
    total += arry[i];
}</span><span id="d37e" class="dh io ga ei ip b de it iu iv iw ix ir w is">var avg = total/noofElements; 
console.log(avg);</span>

Alternate methods

Using Foreach loop

function calculateAverage(array){
    var total = 0;
    var count = 0;</span> <span id="090d" class="dh io ga ei ip b de it iu iv iw ix ir w is">array.forEach(function(item, index){
        total += item;
        count++;
    });</span> <span id="2895" class="dh io ga ei ip b de it iu iv iw ix ir w is">return total / count;
}</span><span id="8538" class="dh io ga ei ip b de it iu iv iw ix ir w is">console.log(calculateAverage(arry));</span>

Using jQuery

var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var total = 0;
var count = 0;</span><span id="514b" class="dh io ga ei ip b de it iu iv iw ix ir w is">jQuery.each(arry , function(index, value){
    total += value;
    count++;
});</span><span id="ad23" class="dh io ga ei ip b de it iu iv iw ix ir w is">console.log(total / count);</span>

Using a function

function calculateAverageOfArray(array){
    var total = 0;
    var count = 0;</span> <span id="19fc" class="dh io ga ei ip b de it iu iv iw ix ir w is">jQuery.each(arry , function(index, value)
    {
        total += value;
        count++;
    });</span> <span id="3cb6" class="dh io ga ei ip b de it iu iv iw ix ir w is">return total/count;
}</span><span id="f5bb" class="dh io ga ei ip b de it iu iv iw ix ir w is">console.log(calculateAverageOfArray(arry));</span>

Using a class

class Avg{
    constructor(){}</span> <span id="9780" class="dh io ga ei ip b de it iu iv iw ix ir w is">static average(array){
        var total = 0;
        var count = 0;</span> <span id="8725" class="dh io ga ei ip b de it iu iv iw ix ir w is">jQuery.each(array, function(index, value){
            total += value;
            count++;
        });</span> <span id="9970" class="dh io ga ei ip b de it iu iv iw ix ir w is">return total / count;
    }
}</span><span id="f2d8" class="dh io ga ei ip b de it iu iv iw ix ir w is">var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
console.log(Avg.average(arry));</span>