Explain to me how this code work?

function diffArray(arr1, arr2) {
  return [
    ...diff(arr1, arr2),
    ...diff(arr2, arr1)
  ]
  
  function diff(a, b) {
    return a.filter(item => b.indexOf(item) === -1);
  }
}

this link of exercise:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays

Which part don’t you understand?? The ES6 feature used in the return statement of the function diffArray is called array destructuring the spread operator, you can read up more on it on google.

And the filter array method is also quite easy to understand, you can read more about it on MDN or in dozens of articles.

If you have a question about some particular part of the code that you don’t understand or some thing that confuses use it would help a lot if you were able to articulate that so we don’t have to guess :stuck_out_tongue: Thanks!

That’s just a spread operator, copying the elements returned by function diff() in a new array, destructuring is a different thing

1 Like

a ha, I don’t understand about

return [
    ...diff(arr1, arr2),
    ...diff(arr2, arr1)
  ]

and why after return still function ???

It is returning an array that contains all the elements in the array returned by the diff function. The spread operator ... copy the elements one by one, so you get a copy of the two arrays. In this case it is the same as writing diff(arr1, arr2).concat(diff(arr2, arr1))

Goddamn. Just woke up from a dream in which i realized that i mixed up the definition of the feature. Thank you for correcting me :slight_smile: