0

How do I calcualte the overall average of an array like this: example array:

[[80, 90, 70], [70,80,60],[90,100,80]]

what I am trying right now

for (let i = 0; i < grades.length; i++) {
  for (let y = 0; y < grades[i].length; y++) {
    lastTotalScore += grades[i[y]];
    lastAverageScore = lastTotalScore / grades[i].length;
    overallTotalScore += lastAverageScore;
    overallAverageScore = overallTotalScore / grades.length;
  }
}

Thanks

0

5 Answers 5

1

flatten the array, and then reduce over the elements adding them up, and then dividing by the length of the flattened array.

const arr = [
  [80, 90, 70],
  [70, 80, 60],
  [90, 100, 80]
];

// Flatten the nested arrays
const flat = arr.flat();

// `reduce` over the numbers, and then divide
// by the number of elements in the array
const avg = flat.reduce((acc, c) => {
  return acc + c;
}, 0) / flat.length;

console.log(avg);

Sign up to request clarification or add additional context in comments.

Comments

1

Calculate the total from all numbers. Divide it with length of total numbers.

Working Sample

const grades = [[80, 90, 70], [70, 80, 60], [90, 100, 80]];
let lastTotalScore = 0;
let length = 0;
for (let i = 0; i < grades.length; i++) {
    length += grades[i].length;
    for (let y = 0; y < grades[i].length; y++) {
        lastTotalScore += grades[i][y];
    }
}
console.log(`Average = ${lastTotalScore / length}`);

OR

Convert the two dimentional array to a linear structure using Array.flat and calculate its sum by looping with Array.reduce and divide by its length.

Working Sample

const grades = [[80, 90, 70], [70, 80, 60], [90, 100, 80]];
const flatGrade = grades.flat();
const sum = flatGrade.reduce((acc, curr) => acc + curr, 0);
console.log(`Average = ${sum / flatGrade.length}`);

Comments

0

This would also work. Using flat and reduce.

const input = [
  [80, 90, 70],
  [70, 80, 60],
  [90, 100, 80],
];

const { sum, count } = input.flat().reduce(
  (prev, curr) => {
    prev.sum += curr;
    prev.count += 1;
    return prev;
  },
  { sum: 0, count: 0 }
);

console.log(sum / count);

Comments

0

As some of the others suggested, flattening the array does the trick nicely. The only additional thing I would consider is to wrap the code into a function, which can then be reused, to keep things DRY.

const someArray = [[80, 90, 70], [70, 80, 60], [90, 100, 80]];

function getAverage(arr) {
    const oneArr = arr.flat();
    const avg = oneArr.reduce((sum, value) => sum += value) / oneArr.length; 
    return avg;  
}

console.log(`The average grade is: ${getAverage(someArray)}.`);

Comments

0

Here's one that uses only reduce.

const arr = [
  [80, 90, 70],
  [70, 80, 60],
  [90, 100, 80],
];
const overallAverage = arr.reduce((acc, curr) => acc + curr.reduce((a, c) => a + c) / curr.length, 0) / arr.length;
console.log(overallAverage);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.