Javascript Array Multidimensional jagged arrays

Introduction

A jagged array is an array where the rows in the array may have a different number of elements.

One row may have three elements, while another row may have five elements.

let grades = [[89, 77],[76, 82, 81],[91, 94, 89, 99]]; 
let total = 0; /*  ww  w  .j av  a  2  s .  c o  m*/
let average = 0.0; 
for (let row = 0; row < grades.length; ++row) { 
   for (let col = 0; col < grades[row].length; ++col) { 
      total += grades[row][col]; 
   } 
   average = total / grades[row].length; 
   console.log("Student " + parseInt(row+1) + " average: " + 
         average.toFixed(2)); 
   total = 0; 
   average = 0.0; 
} 



PreviousNext

Related