Iterate Through All an Array's Items Using Nested For Loops

Tell us what’s happening:
I’ve figured out the solution to this problem using one for loop.

But my initial inclination was to use a nested for loop since I was dealing with nested arrays. I don’t understand why I get 3 outputs of the passing array in the console.

Your code so far

function filteredArray(arr, elem) {
  let newArr = [];
    for (let i = 0; i < arr.length; i++){
      for (let k = 0; k < arr[i].length; k++){
        if (arr[i].indexOf(elem) == -1){
  return newArr;

// change code here to test different cases:
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:

just to be clear , can you clarify what you meant by “I get 3 outputs” by cutting and pasting the console results into your original question? thanks.

In your case, the nested loop operation repeats the search arr[i].length - 1 times. So, if one array passed the filter, it will be added as many times as the inner loop runs.

Just look at what the inner loop is doing.

in the outer loop you are considering the length of arr(which is 4 in this case) and in the inner loop you are looping through and are taking the length/the no of elements inside all 4 arrays(that is 3 in all 4 arrays ). when the i value is 0, inner loop runs until the value of 2 (i.e 3 times(0,1,2) as the condition is k<3) before it terminates when k becomes 3 and then the i value is incremented by 1 and the same process is repeated.