Seek and Destroy filter()

Seek and Destroy filter()
0

#1

Tell us what’s happening:

They tell us to use filter() but I’m having trouble using it to filter out anything where arr[i]===args[j].

Any tips on how to use filter correctly?

Thanks

Your code so far

function destroyer(arr) {
  // Remove all the values
  var args=arr.slice.call(arguments,1);
  var newArr=[];
  for (i=0; i<arr<length; i++) {
    for (j=0; j<args.length; j++) {
      newArr = arr.filter(Boolean(arr[i]===args[j]));
    }
  }
  
  return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Your browser information:

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

Link to the challenge:


#2

First you have a syntax error with:

  for (i=0; i<arr<length; i++) {

I assume you meant to put arr.length. Even if you do, it is not going to solve the challenge. Once you fix this typo above, you need to review the syntax of the filter function. It will look like:

newArray arr.filter(function(arrElement) {
  // code using arrElement
});

#5

Okay I see what I need to do now, but can’t quite think of anything except arr[I]===args[j] .
Because of scope I cant reference array elements within the
This is all I can come up with so far.
Still wrong though and looks like bad code.
function destroyer(arr) {
// Remove all the values
args=arr.slice.call(arguments,1);
var newArr=[];
for (i=0; i<arr.length; i++) {
for (j=0; j<args.length; j++) {
newArr = arr.filter(function(arrElement){
if (arr[i]===args[j]) {
arrElement=args[j];
return arrElement;
}
}
);}
}

return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);


#6

You are not understanding what arrElement represents in the anonymous callback function use in the filter. The first argument of the filter function’s callback function is the current arr’s element during each iteration of arr. So if I had the following code:

var arr = [1,2,3,1,2,3];
var newArr = arr.filter(function(arrElement) {
  return arrElement !== 2 && arrElement !== 3;
});
console.log(newArr);

The above code would display the following to the console:

[1, 1]

Also, I think you need to first write out your algorithm in plain language before trying to code it. You do not have to use filter, but can be use in a possible solution to this challenge.

If you do decide to use filter, then review the MDN documentation for it.


#7

Okay I understand your example.

I think I just need to keep trying to understand what the code is doing every time I make a wrong code.

E.g. my code now returns [3,3].


function destroyer(arr) {
  // Remove all the values
  var args=arr.slice.call(arguments,1);
  var newArr=[];
  for (i=0; i<arr.length; i++) {
    for (j=0; j<args.length; j++) { 
      newArr = arr.filter(function(arrElement) {
        if (arr[i]===args[j]) {
        return arrElement ===args[j];
        }
       });
    
    }
  }
  
 return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);


#8

Put the following console.log statements inside the callback function before the if statement, so you can what the values are of the various variables and expression comparisons. I think this will make you rethink using two for loops and the filter together.

      newArr = arr.filter(function(arrElement) {
        console.log('the current value of arrElement is ' + arrElement);
        console.log('arr['+i+'] = ' + arr[i] + ' and arr['+j+'] = ' + arr[j]);
        console.log('comparison of arr['+i+'] === args['+j+'] evaluates to ' +(arr[i]===args[j]) + '\n');
        if (arr[i]===args[j]) {

and then look at the browser’s console (Ctrl+Shft+J in Chrome), so you can see what is happening inside the callback function during the for loops’ iterations.


#9

Thank you so much for your help!! Do you have your own course? you’re a great teacher!
Finally managed to create a solution but without both for loops.

function destroyer(arr) {
  // Remove all the values
  var args=arr.slice.call(arguments,1);
 
  
 var newArr = arr.filter(function(arrValue) {
    return args.includes(arrValue)==false; 
       
    });
  
  return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

I’m still keen to see if there a way to do it with a for loop. I now understand that I don’t need a for loop for arr as that is part of arr.filter().
However when I did console log it showed true for some things that should be false and visa versa.

Should I use the for loop outside or inside the filter function? And can I reference args[i] in the filter function?

Thank you!


#10

Congratulations on coming up with a very nice solution to this challenge!

Any solution using map, filter, or reduce can be solved with a for loop or while loop.

Since you have already solved the challenge, I will show you first how to solve it with just for loops:

function destroyer(arr) {
  // Remove all the values
  var args=arr.slice.call(arguments,1);
  var newArr=[];
  for (i = 0; i < arr.length; i++) {
    var include = true;
    for (j = 0; j < args.length; j++) {
      if (arr[i] === args[j]) {
        include = false;
        break; // breaks out of the inner for loop
      }
    }
    if (include) {
      newArr.push(arr[i])
    }
  }
  return newArr;
}

and then with one for loop and using the includes function (similar to your solution):

function destroyer(arr) {
  // Remove all the values
  var args=arr.slice.call(arguments,1);
  var newArr=[];
  for (i = 0; i < arr.length; i++) {
    if (!args.includes(arr[i])) {
      newArr.push(arr[i])
    }
  }
  return newArr;
}