Basic Data Structures: Iterate Through All an Array's Items Using For Loops question

Hello. I have this code:

function filteredArray(arr, elem) {
  let newArr = [];
  let index2;
  console.log(elem);
  // change code below this line
  for (let i=0; i < arr.length; i++){
     for(let y=0; y < arr[i].length; y++){
       let index = arr[i].indexOf(elem);
      if(index >=0){
       let index2 = arr.indexOf(arr[i]);
      }
       if(index2 >=0){
         arr.splice(index2, 1);
       }       
     }        
  }

  newArr = arr;

  // change code above this line
  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));

In the console I get: 3
[ [ 3, 2, 3 ], [ 1, 6, 3 ], [ 3, 13, 26 ], [ 19, 3, 9 ] ]

and I get errors:

// running tests

filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)

should return

[ [10, 8, 3], [14, 6, 23] ]
filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)

should return

[ ["flutes", 4] ]
filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")

should return

[ ["amy", "beth", "sam"] ]
filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)

should return

[ ]

Any ideas of what I am doing wrong?

You need to add some console.log statements to your solution.

The first one should be:

console.log(index2);

Put the above line right before the following if statement:

if(index2 >=0){
  arr.splice(index2, 1);
}  

Once you see the value of index2, why do you think that it has that value?

I don’t know.

console.log(index2) produces these:

undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined

It is because you do not understand what let is doing here. What is the difference between var and let? Do some research and then see if you can figure out how to correct this particular issue.

var is more global than let. Let survives in if, for etc only, while var would survive within the whole function. Isn’t that it?

You will want to look into the scope of block-level variables. Think about what block index2 is declared in and where you have access to this variable.

I changed my code and got rid of the lets and used var instead. Now this is my code:

function filteredArray(arr, elem) {
  let newArr = [];   
  console.log(elem);
  // change code below this line
  for (var i=0; i < arr.length; i++){
     for(var y=0; y < arr[i].length; y++){
       var index = arr[i].indexOf(elem);
      if(index >=0){
       var index2 = arr.indexOf(arr[i]);
      }
      console.log(index2);
       if(index2 >=0){
         arr.splice(index2, 1);
       }       
     }        
  }

  newArr = arr;

  // change code above this line
  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));

In the console I get:

3
0
0
0
[ [ 19, 3, 9 ] ]

and the errors are:

filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)

should return

[ [10, 8, 3], [14, 6, 23] ]
filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)

should return

[ ["flutes", 4] ]
filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")

should return

[ ["amy", "beth", "sam"] ]
filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)

should return

[ ]

// tests completed // console output 18 undefined undefined undefined undefined undefined undefined 2 3 0 0 0 [ [ 19, 3, 9 ] ] 2 0 0 peter undefined undefined undefined 1 3 0 0 0

Any ideas of what is going on wrong?

do not change an array while you are looping through it, it messes things up - don’t use splice on the array you are looping on

why instead you don’t fill up newArr?

Thank you for your replies.

Here is my new code:

function filteredArray(arr, elem) {
  let newArr = [];   
  console.log(elem);
  // change code below this line
  newArr = arr;
  for (var i=0; i < arr.length; i++){
     for(var y=0; y < arr[i].length; y++){
       var index = arr[i].indexOf(elem);
      if(index >=0){
       var index2 = arr.indexOf(arr[i]);
      }
      console.log(index2);
       if(index2 >=0){

         if(Array.isArray(arr[i])){
         newArr.splice(index2, 1);
         }
       }       
     }        
  }

  
  // change code above this line
  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));

console.log prints:

3
0
0
0
[ [ 19, 3, 9 ] ]

and I get errors:

filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)

should return

[ [10, 8, 3], [14, 6, 23] ]
filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)

should return

[ ["flutes", 4] ]
filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")

should return

[ ["amy", "beth", "sam"] ]
filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)

should return

[ ]

// tests completed // console output 18 undefined undefined undefined undefined undefined undefined 2 3 0 0 0 [ [ 19, 3, 9 ] ] 2 0 0 peter undefined undefined undefined 1 3 0 0 0

Any input please of what I am doing wrong?

you are still changing the input array
my suggestion is to not use splice at all
instead of removing items from the array, try adding the right ones to an empty array

Thanks for your reply.

This is my code now:

function filteredArray(arr, elem) {
  let newArr = [];   
  console.log(elem);
  // change code below this line
  for (var i=0; i < arr.length; i++){
     for(var y=0; y < arr[i].length; y++){
       var index = arr[i].indexOf(elem);
      console.log(index);   
     }        
        if(index >=0){                       
        newArr.push(arr[index]);        
       }    
  }

  
  // change code above this line
  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));

This is what the console prints:

3
0
0
0
2
2
2
0
0
0
1
1
1
[ [ 3, 2, 3 ], [ 3, 13, 26 ], [ 3, 2, 3 ], [ 1, 6, 3 ] ]

These are my errors:

filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)

should return

[ [10, 8, 3], [14, 6, 23] ]
filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)

should return

[ ["flutes", 4] ]
filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")

should return

[ ["amy", "beth", "sam"] ]
filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)

should return

[ ]

// tests completed // console output 18 -1 -1 -1 -1 -1 -1 1 1 1 3 0 0 0 2 2 2 0 0 0 1 1 1 [ [ 3, 2, 3 ], [ 3, 13, 26 ], [ 3, 2, 3 ], [ 1, 6, 3 ] ] 2 1 1 -1 -1 1 1 peter -1 -1 -1 2 2 2 3 0 0 0 2 2 2 0 0 0 1 1 1

Any ideas of what I am doing wrong?

you are using both indexOf and looping over the array
there is any reason for you to do both? also because you never you y variable anywhere

Thanks for the reply.

Here is my updated code:

function filteredArray(arr, elem) {
  let newArr = [];   
  console.log(elem);
  // change code below this line
  for (var i=0; i < arr.length; i++){
     
       var index = arr[i].indexOf(elem);        
           
        if(index >=0){                       
        newArr.push(arr[index]);        
       }    
  }

  console.log(newArr);
  // change code above this line
  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));

Here is what console.log() prints:

3
[ [ 3, 2, 3 ], [ 3, 13, 26 ], [ 3, 2, 3 ], [ 1, 6, 3 ] ]
[ [ 3, 2, 3 ], [ 3, 13, 26 ], [ 3, 2, 3 ], [ 1, 6, 3 ] ]

And here are my errors:

// running tests

filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)

should return

[ [10, 8, 3], [14, 6, 23] ]
filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)

should return

[ ["flutes", 4] ]
filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")

should return

[ ["amy", "beth", "sam"] ]
filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)

should return

[ ]

// tests completed // console output 18 [ [ 14, 6, 23 ] ] 3 [ [ 3, 2, 3 ], [ 3, 13, 26 ], [ 3, 2, 3 ], [ 1, 6, 3 ] ] [ [ 3, 2, 3 ], [ 3, 13, 26 ], [ 3, 2, 3 ], [ 1, 6, 3 ] ] 2 [ [ ‘flutes’, 4 ], [ ‘flutes’, 4 ] ] peter [ undefined ] 3 [ [ 3, 2, 3 ], [ 3, 13, 26 ], [ 3, 2, 3 ], [ 1, 6, 3 ] ]

Any suggestions of what is going wrong?

Thanks, I finally solved it.

1 Like

awesome! happy coding!

Hi all,

I tried the following code but failed. It’s unclear to me why.
Would you have an advise to make it using >=0?

******************* START CODE *********************

function filteredArray(arr, elem) {
  let newArr = [];
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].indexOf(elem) >= 0) {
        break
      };
      newArr.push(arr[i]);  
    };
  return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

********************* END CODE ********************

Thanks ahead
++

@Kleyer

break

The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

If you terminate the loop you are not going to check all the nested arrays.

// finds 2 and breaks the loop never pushing ["flutes", 4]
filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)
Code
function filteredArray(arr, elem) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].indexOf(elem) >= 0) {
      console.log(arr[i]); // [ 'trumpets', 2 ]
      break;
    }

    console.log(arr[i]); // nothing is logged

    newArr.push(arr[i]);
  }
  return newArr;
}

filteredArray(
  [
    ['trumpets', 2],
    ['flutes', 4],
    ['saxophones', 2],
  ],
  2
);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums