Diff Two Arrays (code help)

I cant understand where i went wrong, can anyone help me with this??

function diffArray(arr1, arr2) {
var newArr = [];
newArr = arr2.filter(a1 => arr1.filter(a2 => a1!=a2));
return newArr;

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

Challenge: Diff Two Arrays

Link to the challenge:

Can you explain your logic here? How do you expect it to work? Can you break it down for me?

main goal :

The values in arr2 will be compared with that of arr1.

process :

First arr2 will pass a value to a1, which then invokes the other filter command. now arr1 will pass a value to a2.
The values from arr1( which is a2) and value from arr2( which is a1) will be compared and the a2 will be returned from the 2nd filter command, when a1 is not equal to a2.
and this way all the values will be evaluated one by one.

I don’t think you understand how filter() works.
It puts every value in the array through the callback, which should return a boolean. The result will be an array containing those values that resulted in a true return from the callback function.

OK, i think i am starting to realize whats wrong here. Thanks alot…again

You can do this!

1 Like