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.
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;
}
}
);}
}
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?