Symmetric Difference project problem

Symmetric Difference project problem
0

#1

Tell us what’s happening:
This code will take any number of arrays as parameters into the sym function. In my testing, I am sending in 2 arrays. So, I load each array into the args variable. Then, I pass each args array along with an array which will contain the results of the called function to the function called theDifference. This all seems to work correctly except for one issue: for some reason, the resultArray ends up containing a comma. Very strange. Here is the output that is written to the console:

start theDifference <— This is the start of theDifference the first time it’s called
array1 = 1,2,3
resultArray =
test the value of element = 1
test the value of element = 2
test the value of element = 3
end the Difference
at the end of the function, resultsArray = 1,2,3 <- this looks good so far
start theDifference <— This is the start of theDifference the second time it’s called
array1 = 5,2,1,4
resultArray = 1,2,3, <---- notice the comma at the end of this array? where does this come from?
test the value of element = 5
test the value of element = 2
test the value of element = 1
test the value of element = 4
end the Difference
at the end of the function, resultsArray = 1,2,3,5,4 <---- notice the two commas between 3 and 5?
the results = 1,2,3,5,4, <---- notice the comma after the 4? Where does this come from?


So, for some reason, even though the returned array does not show a comma at the end of the console.log at the end of theDifference function, a comma is inserted for some reason.

Any assistance is greatly appreciated.

Jonathan

Your code so far

    function theDifference(array1, resultArray){
  
      console.log("start theDifference");
      console.log("array1 = " + array1);
      console.log("resultArray = " + resultArray);

      array1.forEach(function(element) {
        console.log("test the value of element = "+element);
        if (resultArray.indexOf(element)<0 && resultArray.indexOf(element<0)){
          resultArray.push(element);
        }
    
      });  
  
      console.log("end the Difference");
      console.log("at the end of the function, resultsArray = " + resultArray);
      return resultArray;
  
  }
  function sym() {
  
      var args = [];
      var theResults = [];
      var loop;
      var l;
  
      for (loop=0;loop<arguments.length;loop++){
        args[loop] = arguments[loop];
      }
    
      for (l=0;l<args.length;l++){
        theResults.push(theDifference(args[l],theResults));
      }
    
      console.log("the results = " + theResults);
  
      console.log("------------------");
  
      return theResults;
    }

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

Link to the challenge:


#2

The following line passes in theResults to the resultArray argument of the theDifference function (so they refer to the same array). Then inside the theDifference function, you push elements into resultArray, which is really pushing elements the same array as theResults. The theDifference function returns resultArray (which again refers to the same array theResults) and then this array is pushed back into theResults. What this really does is create a circular reference, because it is pushing itself into the array for each argument originally passed into the sym function. This is causing major problems.

theResults.push(theDifference(args[l],theResults));

Your algorithm in general is not correct. Can you describe the algorithm you were attempting implement with code?


#3

As an illustration of randelldawson’s point, if you remove the push call from within your second for loop, you’ll get the correct answer.

      for (l=0;l<args.length;l++){
        theDifference(args[l],theResults);
      }

However, this is still a bit misleading. Returning resultArray from within theDifference is not doing anything. (You could comment that out.) You’re passing a reference to theResults to theDifference and then pushing to that array in your forEach callback, so you don’t need to return anything from theDifference. Is it possible that you had the logic of theDifference inside the for loop to begin with and then got mixed up when you broke it out into a separate function?

Also, resultArray.indexOf(element<0) is unnecessary.

Basically, just removing theResults.push( will get the correct results, at least for this initial test, but thinking through randell’s answer would be really helpful, long term.