Would someone please review my code from challenge - Symmetric Difference

Would someone please review my code from challenge - Symmetric Difference
0

#1

Here’s the link to the challenge.
It took me 2 hours to find a solution, and I would like to have my code reviewed for improvements on readability and simplification.

Thanks!

function sym(args) {
  // Creating an array from the arguments object
  var arr = Array.from(arguments);
  // I figured that it would be the easiest to use an sepperate array for the first set to be tested
  var arrCheck1 = arr.shift();
  //this array acts as a receiver for the symmetric difference
  var arrCheck2 = [];
  //this function filters the symetric difference from "arrCheck1"
  function reduceCheck(accumulator, element){
    if (arr[0].indexOf(element) == -1 && accumulator.indexOf(element) == -1){
      accumulator.push(element);
    }
    
    return accumulator;
  }
  // this function filters the symetric difference from the 1st element of "arr"
  function reduceArr(accumulator, element){
    if (arrCheck1.indexOf(element) == -1 && accumulator.indexOf(element) == -1){
      accumulator.push(element);
    }
    return accumulator;
  }
  // This loop loops through "arr", assigning the symmetric difference it has with "arrCheck1" to "arrCheck2", and if there are more than 2 sets in "arr" to be filtered, it assigns the symmetric difference to "arrCheck1" and removes the 1st element from "arr"
  while (arr.length > 0){
    arrCheck2 = arrCheck1.reduce(reduceCheck, []).concat(arr[0].reduce(reduceArr, []));
    arrCheck1 = arrCheck2;
    arr.shift();
  }
  return arrCheck2;
}

#2

Well, I can certainly weigh in with my highly subjective analysis. Take it as food for thought rather than gospel.

  1. For readability, don’t document your code with comments.
  2. arr, arrCheck1, and arrCheck2 aren’t the most descriptive names, which makes the code harder to skim.
  3. Having the definitions for reduceCheck and reduceArr above sym‘s logic incurs some cognitive penalty for the reader who must figure out which part of this function is actually doing the symmetric differencing. Those inner functions’ names could be more descriptive as well.
  4. You’re using a while loop, but the rest of your code uses higher order functions. This isn’t necessarily bad, but a great example of a code smell and a candidate for refactoring.
  5. This is confusing and inelegant:
    arrCheck2 = arrCheck1.reduce(reduceCheck, []).concat(arr[0].reduce(reduceArr, []));
    arrCheck1 = arrCheck2;

Regardless, congrats on finish the challenge!