I am looking for assistance with a coding challenge involving an array of numbers. The goal is to calculate the average of every 3 elements in the array and store these averages in a new array.
Below is the code I currently have:
var total = 0;
//Array containing the numbers
for(i=0; i < arr.length; i++) {
total += arr[i];
}
var avg = total / arr.length;
avgArray.push(Math.round(avg));
However, this code only gives me the average of all the elements in the array. I need help modifying it so that it calculates the average of every set of 3 elements instead.
For example, the avgArray should display: - Number 1: Average of first 3 elements - Number 2: Average of the second 3 elements - Number 3: Average of the third 3 elements ... Can you please provide guidance on how to achieve this?