Error: cannot read property of 'length' undefined

Tell us what’s happening:
I Think my logic is correct then what is wrong with the code
Your code so far


function largestOfFour(arr) {

var large = [];
for(var i = 0; i <= arr.length; i++)
{
  for(var j = 0; j <= arr[i].length; j++)
  {
      large.push(j);
  }
}

return large;
}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

There are two potential places where that error can originate (both uses of .length). Finding which one is it will help to figure out why that’s happening. Just before either you could add the same call wrapped in console.log() and check if what’s output to console clears anything.

Your array has a length of 4, and your loop will currently run for i=4. One problem is that arr[4] does not exist.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.