What purpose does the [0] serve here?

Tell us what’s happening:
In the return part of this function, if I remove the [0] it will return the first array, [4, 5, 1, 3]. But how come if I add the [0] to it, this returns the number of arrays within the array? I half understand it has something to do with it being an index, because when I replace it with 3 it will still return 4, but when I replace it with 4 it will return undefined because the index of the array goes up to 3, not 4.

  **Your code so far**

function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
  return i = arr[i][0]
  }
}

console.log(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/87.0.4280.141 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

1 Like

arr[i] is each nested array arr[i][0] is the first element inside the nested array.

In the example, you posted you are returning out of the loop and function and you only get to 4 which is the first element in the first nested array. It is not the length of anything.

function largestOfFour(arr) {
  for (let i = 0; i < arr.length; i++) {
    return i = arr[i][0]
    }
  }
  
  console.log(largestOfFour([[42, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])); // 42
3 Likes

Right, so this is an array of arrays. We often call it a two-dimensional array, which technically JS doesn’t have, but it’s a good way to think of it. So, think of this as a two dimensional table of data.

4    5    1    3
13   27   18   26
32   35   37   39
1000 1001 857  1

So, this is your table, rows and columns. If you ask for row 0 (assuming zero indexing), you will get “4 5 1 3”. If you ask for row 0, column 0, you will get 4. Perhaps instead of “column” you should say “element”. This can be a way to think of what is happening here.

To translate this into JS, each row is an array and the entire thing is an array of those arrays. If I tell it arr[x][y], I am telling it to go to array x and go into that row (array) to the y th element.

2 Likes

Now I understand it. Since i=0, it is referencing index 0 of all the arrays, and [0] is referencing the first element within that sub-array. If I change it to let i = 1, it will return 13, and let i = 2 will return 32, and so on. And if i = 0 and I do return i = arr[i][1], it will print 5, and so on. Makes perfect sense, thank you.

It sounds like you have it. But yeah, it’s a weird way to think.

And again, JS doesn’t truly have two dimensional arrays (some languages do) but they are often used this way, depending on your data needs. And for this set of data, thinking of it as a two dimensional array makes sense. For other implementations, the logic would be the same, even if the visualization starts to break down.

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