Iterate Through Array Items Using For Loops

I can get the answers right in chrome console. I don’t know what the problem is. Thank you!

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:

I’m pretty sure you don’t need the else continue. (the loop continues anyway).
But more important, your for loop dies when you break out of it. So it ends when you find the elem for the first time. The elem can occur in multiple arrays, and you have to remove them all.

But it doesn’t clear the first test either, wherein the number is not repeated in any other array.

Your structure of the return value is nested nested array. Try making it nested array. Also, you are supposed apply the filter for each nested array. Right now your filter only applies to the first nested array that meets the if condition.

How’s the return value nested nested?
And yes, I am stopping it after one but the code doesn’t clear the test case where the number is not repeated either.Like this one:
[[10, 8, 3], [14, 6, 23], [3, 18, 6]]
Here the element we’re supposed to delete the container of is 18. And 18 appears just once so my code should still work. :frowning: Please help

Your current code breaks after the first iteration, so ind = 0. Your splice removes the first subarray [3, 2, 3], then for some reason you nested the entire arr into another array named newArr. So now you have a 3-dimensional array instead of a two-dimensional array which is what you are supposed to have.

Instead of mutating arr with splice, why not modify your if statement so that if your indexOf returns -1, then push the subarray ( arr[i] ) into newArr?

arr.splice() is applied to the arr which is the nested array argument. So, its return value is also a nested array.
Suppose the arr after the splice looks like [ [1, 2], [3, 4] ], then ...arr results [1,2] [3,4]. Since you are putting that to another array, [...arr] is [ [1, 2], [3, 4] ]; this is what you are pushing to newArr.

newArr then becomes [ [ [1, 2], [3, 4] ] ] , which is nested nested array.

That’s actually a really good approach, thanks.
Here’s my code inside the if statement:
Still didn’t pass any test cases.

You don’t want to spread the subarray out into individual elements. You just want to push the whole subarray.

I think I misunderstood the spread operator.
If arr=[1, 2, 3, 4]; And if I were to add that to another array, anotherArr.
Can I do that? It seems to work well on the chrome console.
Or this:

Actually I did do that, it made no difference to the test cases:

If you want to add arr to another array, then you just do anotherArr.push(arr).
anotherArr = arr is just overriding.

The spread operation is simple as this example.

let a = [1, 2, 3, 4]

// [...a] => [1, 2, 3, 4]

Post your latest code, so we can see the entire thing.

Apologies. I thought the compiler was stuck, but it was just the page that wasn’t responding. I reloaded it and it works now.

Final code:

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

  for(let i=0; i&lt;arr.length; i++){

  return newArr;

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)); 

Thanks a lot, for walking me through this. :smiley:

1 Like

Thank you for being patient with me. :slight_smile: Appreciate it. Am clear.

@RandellDawson hey that idea of using push worked out great, thanks!
@graven_whismas try flipping your test case of the if statement. remember you want to push in the items from the array that do not have the elem inside them.

This worked and passed.

for (let r=0; r < arr.length; r++) {
if (arr[r].indexOf(elem) < 0) {