Finding Mean at Every Point in JavaScript Array

We have given the array of numbers and our task is to find the mean at every position in the array using JavaScript. Below we have added the example for a better understanding of the problem statement.

Example:

Input: arr = [1, 2, 3, 4, 5]
Output:
Mean at position 1: 1
Mean at position 2: 1.5
Mean at position 3: 2
Mean at position 4: 2.5
Mean at position 5: 3
Explanation:
Cumulative Sum: 1, Mean: 1/1 = 1
Cumulative Sum: 1 + 2 = 3, Mean: 3/2 = 1.5
Cumulative Sum: 1 + 2 + 3 = 6, Mean: 6/3 = 2
Cumulative Sum: 1 + 2 + 3 + 4 = 10, Mean: 10/4 = 2.5
Cumulative Sum: 1 + 2 + 3 + 4 + 5 = 15, Mean: 15/5 = 3

We can find this mean using three different methods which are stated below:

Table of Content

  • Using For Loop
  • Using the Array Map Method
  • Using the Array Reduce Method

Using JavaScript For Loop

In this approach, we are using the for loop to iterate over the input array and we are calculating the mean at each position and printing the mean at each point using the console.log function.

Example: In this example, we will find the mean at every point in JavaScript using For Loop.

Javascript
const arr = [1, 2, 3, 4, 5];
let s = 0;
for (let i = 0; i < arr.length; i++) {
    s += arr[i];
    const res = s / (i + 1);
    console.log(`Mean at position ${i + 1}: ${res}`);
}

Output
Mean at position 1: 1
Mean at position 2: 1.5
Mean at position 3: 2
Mean at position 4: 2.5
Mean at position 5: 3

Using the Array Map Method

In this method, we are using the map function wot compute the mean at each potion in the input array by updating the cumulative sum and storing the means in the means array.

Example: In this example, we will find the mean at every point in JavaScript using the Array Map Method

Javascript
const arr = [5,4,3,2,1];
let s = 0;
const means = arr.map((value, index) => {
    s += value;
    const res = s / (index + 1);
    console.log(`Mean at position ${index + 1}: ${res}`);
    return res;
});

Output
Mean at position 1: 5
Mean at position 2: 4.5
Mean at position 3: 4
Mean at position 4: 3.5
Mean at position 5: 3

Using the Array Reduce Method

In this method, we are using the reduce function to compute the mean at each potion in the array by updating the cumulative sum and storing the mean in the means array.

Example: In this example, we will find the mean at every point in JavaScript using the Array Reduce Method

Javascript
const arr = [1, 2, 3, 4, 5];
const means = arr.reduce((acc, v, idx) => {
    const s = acc.sum + v;
    const res = s / (idx + 1);
    acc.means.push(res);
    console.log(`Mean at position ${idx + 1}: ${res}`);
    return { sum: s, means: acc.means };
}, { sum: 0, means: [] });

Output
Mean at position 1: 1
Mean at position 2: 1.5
Mean at position 3: 2
Mean at position 4: 2.5
Mean at position 5: 3

Method 4: Using a Functional Approach with Array.map() and Array.slice()

In this approach, we use the Array.map() method along with Array.slice() to create a new array containing the means at each position. We iteratively calculate the mean for each position by slicing the array from index 0 to the current position and then computing the sum and mean.

Example:

JavaScript
// Function to find the mean at every position in the array using functional approach
function findMeanAtPosition(arr) {
    return arr.map((_, index) => {
        const slice = arr.slice(0, index + 1); // Slice the array up to the current position
        const sum = slice.reduce((acc, val) => acc + val, 0); // Calculate the sum of the sliced array
        return sum / slice.length; // Calculate the mean
    });
}

// Example usage
const arr = [1, 2, 3, 4, 5];
const means = findMeanAtPosition(arr);
means.forEach((mean, index) => {
    console.log(`Mean at position ${index + 1}: ${mean}`);
});

Output
Mean at position 1: 1
Mean at position 2: 1.5
Mean at position 3: 2
Mean at position 4: 2.5
Mean at position 5: 3