Hi everyone,
I am trying to understand the solution for exercise Find the Symmetric Difference and can’t figure out how the reduce method works in the below code. Could someone please help me understand step by step what is happening?
I understand the code until the reduce method is applied but I have no idea how the reduce method executes the symDiff function even though I think I have a basic understanding of reduce method. How does it know which element to assign to arrayOne and arrayTwo, where is the accumulator here? Basically I understand nothing.
source: freeCodeCamp Challenge Guide: Find the Symmetric Difference
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
function symDiff(arrayOne, arrayTwo) {
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);
}
type or paste code here
Thank you so much!