nanay
June 26, 2021, 5:24pm
1
I just need a deep explanation about my code
Describe your issue in detail here.
**Your code so far**
function destroyer(arr) {
var valsToRemove = Array.from(arguments).slice(1);
return arr.filter(function(val) {
return !valsToRemove.includes(val);
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36
Challenge: Seek and Destroy
Link to the challenge:
That looks more like solution 2 from the hints section than your own code. But anyway.
This line is storing an array of the elements that should be removed in a variable called valsToRemove
(in your example, that’s [2,3]
):
var valsToRemove = Array.from(arguments).slice(1);
The function returns the array that was passed in, with the values to remove filtered out:
return arr.filter(function(val) {
return !valsToRemove.includes(val);
});
Since valsToRemove
is an array, you can use .includes
on it. The filter only lets pass items/returns true
for all the items of arr
that are not included in valsToRemove
.
system
Closed
December 26, 2021, 6:01am
3
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.