DEV Community

tutorials-kept-simple
tutorials-kept-simple

Posted on • Updated on • Originally published at flexiple.com

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.

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.

Table of Contents

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 - Getting average of an array using JavaScript

class Avg{
    constructor(){}

    static average(array){
        var total = 0;
        var count = 0;

        jQuery.each(array, function(index, value){
            total += value;
            count++;
        });

        return total / count;
    }
}

var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
console.log(Avg.average(arry));
Enter fullscreen mode Exit fullscreen mode

Alternate Methods

Using Foreach loop

arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

function calculateAverage(array){
    var total = 0;
    var count = 0;

    array.forEach(function(item, index){
        total += item;
        count++;
    });

    return total / count;
}

console.log(calculateAverage(arry));
Enter fullscreen mode Exit fullscreen mode

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;

jQuery.each(arry , function(index, value){
    total += value;
    count++;
});

console.log(total / count);
Enter fullscreen mode Exit fullscreen mode

Using a function

var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

function calculateAverageOfArray(array){
    var total = 0;
    var count = 0;

    jQuery.each(arry , function(index, value)
    {
        total += value;
        count++;
    });

    return total/count;
}

console.log(calculateAverageOfArray(arry));
Enter fullscreen mode Exit fullscreen mode

Using a class

class Avg{
    constructor(){}

    static average(array){
        var total = 0;
        var count = 0;

        jQuery.each(array, function(index, value){
            total += value;
            count++;
        });

        return total / count;
    }
}

var arry = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
console.log(Avg.average(arry));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)