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

I have spent so long trying to figure out what is wrong with this code and it’s driving me bananas.


function filteredArray(arr, elem) {
  let newArr = [];

  for(var i=0; i<arr.length; i++){
    if(arr[i].indexOf(elem) !== -1){ 
newArr =[...arr];
  return newArr;
// change code here to test different cases:
console.log(filteredArray([[1, 2, 7], [1, 3, 2], [2, 3, 26], [19, 2, 9]], 3));

I misread your function the first time, apologises.

The error comes from the way you splice.
splice start and deleteCount should be indexes, so far you are passing the content of the array


Maybe you meant


edit: I personally don’t like the use of splice on the element you are looping on; since splice changes the content of the given array, you may loose the “integrity” of your array since it may changes its content mid-execution

Thank you so much! I really appreciate it.