Return Largest Numbers in Arrays - Property length of undefined

Return Largest Numbers in Arrays - Property length of undefined
0.0 0

#1

Hi, I badly need an advice for my code. Why System says Cannot read property “length” of undefined:

function largestOfFour(arr) { 
  var largestArray = [];
  for (var i = 0; i < arr.length; i++) {
   var largestNumber = arr[i][0];
    for (var j = 1; j < arr[i].length; i++) {
      if (largestNumber < arr[i][j]) {
        largestNumber = arr[i][j];
      }
   }
    largestArray[i] = largestNumber;
  }
  console.log(largestArray);
  
  return largestArray;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

#2

Because your nested for loop is incrementing I instead of j, eventually arr[i] is no longer a valid array reference, so it becomes undefined.