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

Iterate Through All an Array's Items Using For Loops 00
0

#1

Tell us what’s happening:
Hi, I don’t see what is wrong with my code

Your code so far


function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
for (let i = 0; i < arr.length; i++) {
   // find arrays 
   if(typeof arr[i] === "object") {
     // check if it has elem and remove it
      let holderArray = arr[i];
      for (let j = 0; j < holderArray.length; j++) {
        if (holderArray[j] === elem) {
           // remove holderArray. from arr
           arr.splice(i,1);
        }
      }
   }else if(typeof arr[i] !== "object" && arr[i] === elem) {
        arr.splice(i,1);
   }
  }
  newArr.push(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 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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/


#2

Looks like you are splicing subarray instead of corresponding value in your subarray.


#3

okay, so I must delete the elem?


#4

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.


#5

The code is passing every test except: filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3) should return [ ]


#6

Can you post your new code?


#7

function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
// find arrays
if(typeof arr[i] === “object”) {
// check if it has elem and remove it
let holderArray = arr[i];
for (let j = 0; j < holderArray.length; j++) {
if (holderArray[j] === elem) {
// remove holderArray. from arr
arr.splice(i,1);
}
}
}else if((typeof(arr[i]) !== “object”) && (arr[i] === 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));


#8

Can you format your code?


#9

I can change my code, if you can help me fix it


#10

I can’t help you because your code is not formatted and it’s very difficult to read.

select your code and hit preformat icon in your editor.


#11

function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
// find arrays
if(typeof arr[i] === “object”) {
// check if it has elem and remove it
let holderArray = arr[i];
for (let j = 0; j < holderArray.length; j++) {
if (holderArray[j] === elem) {
// remove holderArray. from arr
arr.splice(i,1);
}
}
}else if((typeof(arr[i]) !== “object”) && (arr[i] === 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));`

  • Preformatted text

`


#12

I am sorry, I am trying to format my code


#13
function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
for (let i = 0; i < arr.length; i++) {
   // find arrays 
   if(typeof arr[i] === "object") {
     // check if it has elem and remove it
      let holderArray = arr[i];
      for (let j = 0; j < holderArray.length; j++) {
        if (holderArray[j] === elem) {
           // remove holderArray. from arr
           arr.splice(i,1);
        }
      }
   }else if((typeof(arr[i]) !== "object") && (arr[i] === 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));

#14

I see now.

After you are splice a subarray, you are skipping the next subarray. (Your index (i) goes up by one in the forloop but a subarray is removed)

Just include i-- in your code after your splice.


#15

I don’t understand why the code is returning the 2nd array


#16

Thanks alot; That is genius