What does my code work for the diff two arrays challenge

function diffArray(arr1, arr2) {
  var newArr = [];
  var arr = arr1.concat(arr2);
   for(var i = 0; i < arr.length; i++) {
  if(arr1.indexOf(arr[i]) === -1 ||arr2.indexOf(arr[i]) === -1) {
    newArr.push(arr[i]);
  }
}
  return newArr;
}

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

I was trying different possible solution and sort of stumbled this solution. Can someone explain to me why it works? Thanks.

Thanks. I was looking at it some more and was starting to understand it more, but your explanation clears up any other doubts.

What is it supposed to do?

Edit: never mind. :blush:

I get that ES6 can’t be used in the FCC editor, so I’m not being to be a smarty-pants, but the first time I wrote a solution to this problem, it took me ~20 lines [0] and I just realised it can be done in one now

function diffArray(a1, a2)[…a1, …a2].filter((e) => (!a1.includes(e) || !a2.includes(e)))

or like this

let diffArray = (a1, a2) => […a1, …a2].filter((e) => (!a1.includes(e) || !a2.includes(e)))

The first one works but doesn’t format correctly in my editor. ES6 is awesome.

[0] because I was bad, not javascript

1 Like