Can I have a clarification?

Tell us what’s happening:

Hi,
I solved this task with the below code but manipulating an fiddling with I can not understand why if you remove the .slice(1) method to the args variable the function works as well. With the slice() args is equal to [2,3], and I can see how filter can loop around these homogeneous arrays. Without the slice(), args is equal to [[ 1, 2, 3, 5, 1, 2, 3 ], 2, 3 ]. How this can be checked in the filter() method against arr without causing problems? I should think that at some point the filter encounters the arr ===args[0] condition and this may evaluate to true so it’ s correctly evaluated and does not appear in the filtered arr. But if I reverse the boolean from return arr.filter(val => !args.includes(val)); to return arr.filter(val => args.includes(val)); the args[0] (an array) does not appear in the result.
Hope I made myself clear.

thank you

Your code so far


function destroyer(arr) {
//let args = Array.prototype.slice.call(arguments);
let args = [...arguments].slice(1);
console.log(args);// [2,3]
console.log(arr);//[ 1, 2, 3, 5, 1, 2, 3 ]
return arr.filter(val => !args.includes(val));
}
//console.log(args);
console.log(destroyer([1, 2, 3, 5, 1, 2, 3], 2,3));//[ 1, 5, 1 ]

Your browser information:

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

Challenge: Seek and Destroy

Link to the challenge:

filter will only work recursively on arrays within arrays

since arr[0] was the only array inside arr, using slice(1) was unnecessary.

1 Like