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

Tell us what’s happening
I have iterated through the nested array and removed the elem argument and on the console it displayed the new array which is [ [ [ 2 ], [ 1, 6 ], [ 13, 26 ], [ 19, 9 ] ] ] but i am not still passing the test can someone please tell me what i’m doing wrong

Your code so far

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
     for (let j = 0; j < arr[i].length; j++) {
       if (arr[i][j] == elem) {
         arr[i].splice(j, 1)
         if (arr[i] == elem) {
           arr[j].splice(i, 1); i--

  // 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/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

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

Link to the challenge:

You don’t want to modify the array that’s passed in because we typically want to preserve that data, which is why newArr is defined. So, you wouldn’t want to call splice on the passed in arr variable because you’re modifying the original data, and that is not the behavior we typically want.

You don’t have to overcomplicate this problem. One of the most efficient ways to solve this is to just use one for loop, the very first one, that iterates over each of the tiny arrays within the larger one. Then, after you check to see if arr[i] is an array, then check to see if arr[i] includes the elem, and if not, push that mini array (arr[i]) into newArr. You can also use indexOf to check for its index, and if its index is < 0, then push the mini array into newArr.

Thanks so much your insight was really helpfull

Absolutely! Happy Coding!

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