Diff Two Arrays Not sure if it's correct

Diff Two Arrays Not sure if it's correct
0.0 0

#1

Tell us what’s happening:
I’ve been trying to solve this problem for a long time and when I thought I made it I got one thing wrong and I don’t really know how to correct it. I’m worried that my whole code might be incorrect and I’m doing it wrong, so if you have a chance please have a look at it and help me to see the right path.
Thanks in advance.

Your code so far

function diffArray(arr1, arr2) {
  var newArr = [];
  var temp;
  var temp5;
  for (var i = 0; i<arr1.length; i++){
  temp = arr1[i];
    if(arr2.indexOf(temp) === -1){
      newArr.push(temp);
    }
    for (var j = 0; j<arr2.length; j++){
  temp5 = arr2[j];
    if(arr1.indexOf(temp5) === -1){
      newArr.push(temp5);
    }
  }
  }
 
  
  // Same, same; but different.
  return newArr;
}

diffArray([], ["snuffleupagus", "cookie monster", "elmo"]);

Your browser information:

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

Link to the challenge:


#2

Your logic is off.

For the example call of diffArray([], [‘snuffleupagus’, ‘cookie monster’, ‘elmo’]);
The outer for loop is designed to iterate through arr1. Since arr1 is a blank array, there is nothing to iterate, so nothing inside the for loop is executed.

Instead of nesting the other for loop inside the first, try keeping them as two separate for loops and see what that does for you.