Bug in my program

Bug in my program
0

#1
function largestOfFour(arr) {
  // You can do this!
  var arr2;
  var i, j ;
  for( i=0 ; i<arr.length ; i++)
    {  
      
      var largest = 0;
      for(j=0 ; j<4; j++)
        {
          if(arr[i][j]>largest)
            {
              largest = arr[i][j];
              
            }
        }
        arr2[i] = largest;
    }
  
  return arr2;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


#3

As the output console shows: TypeError: arr2 is undefined. You are trying to access an item in arr2 through arr2[i] = largest. Using property accessors (like [i]) is only possible on objects and arrays, but javascript has no idea what type arr2 is, because it isn’t set to anything.

You can fix this byy setting arr2 to an empty array:

var arr2 = [];

#4

thanks alot. it was great help.