Challenge "Find the Symmetric Difference"

Hello,
I’m compare my solution with the first displayed on the free code site. I’m facing in this part of the code of the solution that i’m not understanding:

function sym() {
  var args = [];
  for (var i = 0; i < arguments.length; i++) {
    args.push(arguments[i]);
  }
 
  function symDiff(arrayOne, arrayTwo) {
    console.log(arrayOne); //[1,2,5] ;[2,3,5]
    console.log(arrayTwo);//[1,3]; [ 3,4,5]
    var result = [ ];

    arrayOne.forEach(function(item) {
      if (arrayTwo.indexOf(item) < 0 && result.indexOf(item) < 0) {
        result.push(item);
      }
    });

    arrayTwo.forEach(function(item) {
      if (arrayOne.indexOf(item) < 0 && result.indexOf(item) < 0) {
        result.push(item);
      }
    });

    return result;
  }

  // Apply reduce method to args array, using the symDiff function
  return args.reduce(symDiff);
};


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

why in the function “symDiff” arrayTwo displays 'console.log(arrayTwo);//[1,3];3,4,5]… i mean when we call function sym and we pass [1, 2, 5], [2, 3, 5], [3, 4, 5], the val [1,3] there is not inside it…so why it happens?

thanks in advance for any help.

The second time symDiff gets called, the argument arrayOne passed in by reduce is the symmetric difference of the first two items in the array, not the second item in the array.

I would recommend checking the docs for how Array.reduce works.

1 Like