Trouble with Recursive function for Symmetric Difference

I’m trying to create a recursive function that repeatedly joins the first two arrays of the combined array and finds the symmetric difference of those two arrays. I want this process to continue until there is only one array left which should result in the symmetric difference of all of the provided arrays in the combined array.

I am currently having trouble trying to get my function to work. Whenever it runs, nothing is returned yet no errors are seeming to be shown. I just need help to get this recursive algorithm going.

function sym(args) {
  
  var combined = Array.from(arguments);

   //REMOVES ALL REPEATS IN EACH ARRAY ITEM IN COMBINED ARRAY
 for (var i = 0; i < combined.length; i++) {
    var ref = [];
   
    for (var j = 0; j < combined[i].length; j++) {
      if (ref.indexOf(combined[i][j]) == -1) {
        ref.push(combined[i][j]);
      }
    }
   
   combined.splice(i, 1, ref);
   
  }
  //return combined;
  /*----------EVERYTHING ABOVE WORKS FINE------------*/

  
  /* Recursive Function */
  function symDif(arr) {
    
    var newArr = arr; 
    
    if (newArr.length === 1) {
      return newArr[0];
    } else {
      
      var firstTwo = newArr[0].concat(newArr[1]);
      var filtered = firstTwo.filter(function(x) {
         return firstTwo.indexOf(x) === firstTwo.lastIndexOf(x);
      });
      newArr.splice(0,2, filtered);
 
      symDif(newArr);
    }
  }
  
  symDif(combined);
  
  
  
 
}

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

You are not returning anything in your else block of code. You call symDif(newArr), but should be returning symDif(newArr). Also, you call symDif(combined) within your sym function, but you should be returning symDif(combined).

1 Like

Ah ok, I see now. Thank you so much for the response and help. My solution works now!