Problem with finding the length of a nested array

Tell us what’s happening:
Describe your issue in detail here.

I’m getting an error "TypeError: arr[i] is undefined"
There seem to be some problem with the code in the second for loop in the words-
“arr[i].length”
the code should find the biggest number of each array from arr and push it into
new array with all those numbers


function largestOfFour(arr) {
let biggestArr = []
let biggestNum = 0
for (let i = 0; i <= arr.length; i++) {
  biggestArr.push(biggestNum)
  biggestNum = 0
  emphasized textfor (let j = 0; j <= arr[i].length; j++) {
    if (arr[i][j] > biggestNum) {
      biggestNum = arr[i][j]
    }
  }
}
biggestArr.shift()
return biggestArr
}

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

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Just glancing at your code, this may be problematic. Try to think about how this work in the loop. Remember that array indexes are 0 indexed, but lengths start counting at 1. So… the last index is not the lengths, it is [fill in the blank]. That is why you don’t see “<=” when using the loop to index through an array, at least not if you are using the length of the array.

Is that a sufficient hint?

1 Like
for (let i = 0; i <= arr.length; i++) 

should be

for (let i = 0; i < arr.length; i++) 

Because the index goes from 0 to [lenght-1]

[sigh] I was trying to hint him the right direction. You’ve deprived him of the opportunity of learning how to figure it out, a very important skill for a developer. This is a learning site, not a get the answer so you can skip the the next challenge site. Please be respectful that there may people here that want to learn. Or at least give them a chance first.

3 Likes

It’s fine, I found the solution myself :slight_smile: . although I think you are right about that…
BTW can I vote in your Turing test?

1 Like