Beat This Dead Horse: Seek and Destroy

I’ve been struggling with this challenge for days. I’ve read the solution but I’m stuck on the .filter() method. The if (arr.includes(args[i])) {}returns true and I want to filter the args[i] out of arr.

Arrrrrg!

Your code so far


function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
for (var i = 1; i < args.length; i ++) {
		if (arr.includes(args[i])) {
		var array = arr.filter(function(index) {
			array = delete index;
		})
        }
  }console.log(array);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:

For filter() you need to return something from callback function either evaluates to true or false. true would mean that it is kept in the array and false means it won’t be in the returned array.

I’d suggest seeing what your code is currently doing line by line and going over the logic. Use console.log to debug and see what values are if you don’t know what they are at that moment will help.

function destroyer(arr) {
var args = Array.prototype.slice.call(arguments); // this is the arguments now in an array, for this example it would be [[1,2,3,1,2,3], 2, 3]
for (var i = 1; i < args.length; i ++) { // you're going to go through each index of the args array starting from index 1
		if (arr.includes(args[i])) { // if [1,2,3,1,2,3] has the value of args[i] which is 2 on first iteration -> true, and 3 on second iteration -> true
		var array = arr.filter(function(index) { // array is going to be the result of filtering [1,2,3,1,2,3]. actually it's not the indices you're going through, it's the element value here
			array = delete index; // change the value of array to be deleting a value and reassign it to the array variable? 
		})
        }
  }console.log(array);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
1 Like