Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

353
Views
javascript: cómo calcular un promedio en una matriz anidada dentro de una matriz

¿Cómo calculo el promedio general de una matriz como esta: matriz de ejemplo:

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

lo que estoy intentando ahora

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

Gracias

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Calcular el total de todos los números. Divídalo con la longitud de los números totales.

Muestra de trabajo

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

O

Convierta la matriz bidimensional en una estructura lineal usando Array.flat y calcule su suma haciendo un bucle con Array.reduce y divida por su longitud.

Muestra de trabajo

 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 Report

0

flatten la matriz y luego reduce los elementos sumándolos y luego dividiéndolos por la longitud de la matriz aplanada.

 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 Report

0

Esto también funcionaría. Uso plano y reducción .

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!