# Intermediate Algorithm Scripting: Diff Two Arrays: comma operator in binary operation

I don´t understand why my code is not working:

``````function diffArray(arr1, arr2) {

var newArray=arr1.concat(arr2);

console.log(newArray)

var differentArray=newArray.filter(item => (!arr1.includes(item)),(!arr2.includes(item)))

return differentArray;

}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
``````

What do you think that should do in filter? Because afaics even if it worked the way I think you think it works, it still doesn’t quite make sense - one of arr1 or arr2 will always include the item, so you’d get false, and an empty array, every time.

Note that the comma operator does evaluate both statements, but only returns the right hand one. The left hand statement is `item => (!arr1.includes(item))` the right hand one is `(!arr2.includes(item))`. `item` is not something that’s defined for the right hand one, only the left hand one, so you get a reference error: `'item is not defined'`. Note also that I’ve never ever seen the comma operator used outside of golf code, take from that what you may.

1 Like

Okay…So it should be “if item is (false eg, not included) in item 1, return it” then evaluate arr2 the same way, right?

But if I change it to: `item =>(!arr1.includes(item)) || (!arr2.includes(item)`, for instance, wouldn´t that fail because of short circuit evaluation?

It’s going to fail regardless: the issue you have is that you’re trying to do this in one pass*. Because you’re doing this on an array which has all the values concatenated together, there is no way to work out which are unique using your current method. Maybe try filtering both the arrays seperately as a first step?

* doing it in one pass it’s possible (and very simple) but if you want to do that, filter is the wrong tool. I will let you dig around I’m afraid, but just as a hint, JS has some other data structures available (in addition to Arrays and Objects), and one of those is designed for unique values

1 Like

I tried this, and it worked:

``````
var newArray=arr1.concat(arr2);

console.log(newArray)

var differentArray=newArray.filter(item => !arr1.includes(item)||(!arr2.includes(item)))

return differentArray;

}
``````

I don´t get why, though: I thought this would fail because of short-circuit evaluation?

Ah sorry I didn’t read your first response as carefully as I should have, ignore what I said as I’m thinking of a different challenge. Yes, that works - it is going to short circuit, but that works in your favour. If you have

[1, 2,3] and [1, 2]

1. 1 is in first array, is in second array, false
2. 2 is in first array, is in second array, false
3. 3 is in first array, not in second, true
4. Same as 1
5. Same as 2

So only 3 is kept - it’s the only value that is in one but not the other

1 Like

Thanks, I get it now. It´s one of those things I -think- I know but reality is dead set on proving me wrong.

1 Like