Seek & destroy solution not working

that is not doing anything, as filter returns a new array and you do nothing with it, but still, item is not an array

though you can adjust that to work, if you consider that both item and newArgs[i] are array items

I don’t know what to do when I read this it says loop through arguments and filter array where item includes any argument , then return arr

function destroyer(arr, ...args) {
  let newArgs = [...args];
  for (let i = 0; i < newArgs.length; i++) {
    arr.filter(item => arr.includes(newArgs[i]))
  }
  return arr;
}

arr is not changed tho, filter returns a new array

Dilemma is though that I can’t return inside a loop or it’ll stop running right? Not sure what to do

My algorithm steps are since I had to check each item in the array with each argument I decided to loop through the arguments.

I then created this loop to go through the arguments and filter out the array item based on whether or not the array included the arguments.

I believe my original approach didn’t work because I was checking one item in the array vs all arguments as @JeremyLT pointed out

Should I just look up the explanation study it and move on? I’ve been on this for 2hrs

I’m always going to recommend against just looking at and copying the solution.

Ok wont do it :slight_smile: Does it look like I’m getting closer though since your last message? Do you have any suggestions?

The challenge says

Remove all elements from the initial array that are of the same value as these arguments.

The filter and includes methods can help you get here.

But you are using both of those backwards.

let myArray = [1, 2, 3, 4, 5];
console.log(myArray.filter(
  item => item % 2 == 0
)); // What does this print? The filter retains the even or the odds?
let myArray = [1, 2, 3, 4, 5];
console.log(myArray.includes(1)); // true
console.log(myArray.includes(6)); // false

You want to remove any items from the initial array that are included in the rest of the arguments.

This is helpful.

This is less helpful than your original code, because 1) you are not capturing the return value of the filter, 2) filter will keep items that return true.

This would check if they’re even and return those so I should use !arr.includes..?

If this works and eliminates the issue #2, can you help me understand how to return to fix issue #1? Maybe I should know by now but unfortunately I don’t unless I push to an initialized variable and I feel that may not work here

This does what you want if you fix the fact that the includes is backwards. (along with the negation you mentioned)

Like I said, you want to know if the ...args include the item, not if the item includes the ...args.

1 Like

To be clear, you mean includes is backwards and negation as in this:

function destroyer(arr, ...args) {
  return arr
    .concat(...args)
    .filter(item => !arr.includes(item));
}

Since we use concat I used !arr.includes and as you said checked if it contain item, but it’s still not working

Oh I used !args and it worked, but didn’t know I could ignore the …

I’d trim out the concat

1 Like

Ok thank you. Sadly this took me 3hrs but hopefully it’ll pay off in the long haul

Learning code takes time. It’s all part of the process.

1 Like

this is always true, as item comes from arr

what determines if item stays or not? it isn’t something about the other arguments?

That would be filter correct?

filter does the job, but what’s the condition?

but I see you figured it out, good job

1 Like

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