Tell us what’s happening:
I tried solving it with for loops but I failed. So I looked at the FCC solution, and it seems simple, just a concat()
and a filter()
method, but I’m having trouble understanding what is going on.
The concat() method combines arr1 and arr2, but then the filter() method seems too complicated. Why is it checking both arr1 and arr2 for item
? weren’t the two arrays concatenated? If it’s checking both arrays, why concatenate? … Please help me understand this.
Your code so far
function diffArray(arr1, arr2) {
return arr1
.concat(arr2)
.filter(item => !arr1.includes(item) || !arr2.includes(item));
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
.
Challenge: Diff Two Arrays
Link to the challenge: