Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

357
Visualizações
javascript : how to calculate an average in a nested array inside an array

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

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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}`);

about 4 years ago · Juan Pablo Isaza Relatório

0

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);

about 4 years ago · Juan Pablo Isaza Relatório

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda