Basic Data Structures - Iterate Through All an Array's Items Using For Loops: just a general question regarding [.length] method in nested arrays

Tell us what’s happening:
Describe your issue in detail here.
Hi all, I just have a general question regarding using .length in nested arrays…does .length return the number of sub-arrays or the total number of elements present? e.g. for [ [ 2, 1, 3 ], [4, 1, 5], 3 ] would arr.length = 3 (two nested arrays + 3) or 7?

Your code so far

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line

  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15

Challenge: Basic Data Structures - Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

It would return 3, because there are there ‘items’ in the array

1 Like

The great thing about JavaScript is that it runs in the browser. To answer your question, you could have opened up your browser’s dev tools, went to the console, then input the following line and hit enter.

[[ 2, 1, 3 ], [4, 1, 5], 3].length
2 Likes

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