Can someone explain to me more in depth why we use the condition ( .length)… I don’t understand why it is necessary.
Hi @agent007
Welcome to the FCC forum!
In this exercise the .length is used to get the number of items in the array. E.g. this outer array has 3 items:
var arr = [
[1,2], [3,4], [5,6]
];
So arr.length is 3
. The condition i < arr.length
is no longer true when i
becomes 3
, so the loop stops at the last item.
Using the .length like this is useful, because you don’t have to know how many items are on an array. You can set it dynamically … e.g. These sub-arrays have different numbers of items, but it will still work: [[1,2],[3,4],[5,6,7]]
PS: It’s always helpful to post links to the challenges …