Iterate Through All an Arrays Items Using For Loops - DOUBT

Question: We have defined a function, filteredArray , which takes arr , a nested array, and elem as arguments, and returns a new array. elem represents an element that may or may not be present on one or more of the arrays nested within arr . Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed.

  1. filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18) should return [ [10, 8, 3], [14, 6, 23] ]

  2. filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2) should return [ ["flutes", 4] ]

  3. filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter") should return [ ["amy", "beth", "sam"] ]

  4. filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3) should return [ ]

  5. The filteredArray function should utilize a for loop

I don’t really know where I am going wrong…

Console output:
// running tests

arr[i] is undefined

arr[i] is undefined

arr[i] is undefined

arr[i] is undefined

arr[i] is undefined

// tests completed


function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
for(let i = 0 ; i < arr.length ; i++)
{
  for(let j = 0 ; j < arr[i].length ; j++)
  {
    var x = arr[i][j];
    if(x === elem)
    {
     arr.splice(i , 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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops

You are splicing elements out of your array while you are looping over it and that is messing up your indexing - i and j are indexing to something that doesn’t exist after you have removed a subarray.

You might do better to just save the desired subarrays in newArray rather than remove the undesired arrays from arr.

1 Like

Thanks a bunch dude but I didn’t really get you…?

function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for(let i = 0 ; i < arr.length ; i++)
{
for(let j = 0 ; j < arr[i].length ; j++)
{
var x = arr[i][j];
if(x === elem)
{
arr.splice(i , 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));

Generally looping through an array while removing items from that array is going to mess up your indexing. Any fixes to compensate add unnecessary complexity if that can be avoided. That goes doubly for nested loops.

You have a situation where you are trying to test j < arr[i].length for i = 1 but there is no arr[1], you just spliced it out so your code errors

Here’s an example of why this doesn’t always work
Say you have an array you want to remove elements less than 4 so you loop through with a for-loop splicing out any less that 4. Let’s walk through that on [1,2,4,2,1]

[ 1, 2, 4, 2, 1 ] first pass, index is 0, arr[0] is 1, 1 < 4 so splice it out.
[ 2, 4, 2, 1 ] second pass, index is 1, arr[1] is 4, 4!<4 so no splice
[ 2, 4, 2, 1 ] third pass, index is 2, arr[2] is 2, 2<4 so splice it out.
[ 2, 4, 1 ] fourth pass, index is 3, 3 is not < arr.length so loop completes
[ 2, 4, 1 ] Here is resulting array with all numbers less than 4 removed? What? Wait a minute???

The easier approach is to save any elements that are not less than four into a new array and return the new array. When you find a number arr[i] >= 4 you just push it onto new array newArray.push(arr[i])

This way you avoid any issues with changing indexes and your original array remains intact. If you look at the example provided you will see this is the approach used there too.

1 Like

Thanks a lot dude! I got it figured out now!

1 Like