Difference Between Two Arrays help

Difference Between Two Arrays help
0

#1

On the Intermediate Algorithm Scripting: Diff Two Arrays challenge, I am trying to solve it with a for loop (it’s the only way I can kind of do on my own right now). However, I’m getting stuck, and I’m not sure it’s even possible to do it this way, but I would like to see. my junkArr returns everything that is the SAME between the two arrays, and my intention is to have newArr contain everything else, and therefore the correct answer to the challenge. Is there something I can put into newArr.push() that would make that happen? Thanks

function diffArray(arr1, arr2) {
  var newArr = [];
  var junkArr = [];
  for (var i=0; i<arr1.length; i++){
    for(var j=0; j<arr2.length; j++){
      if(arr1[i]===arr2[j]){
        junkArr.push(arr1[i]);
      } else {
        newArr.push();
      }
    }
  }
  return newArr;
}

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

#2

Well, to start, you’re not actually pushing any values. You would have to newArray.push(arr2[j]).

Second this won’t work because your newArr would contain every value in the second array that does not match EACH of the values of the first array. For example, for arrays [a, b, c] and [1,2, 3] you would be doing:

looping through [a,b,c]:
a => check if any elements in 1,2,3 don’t match (1, 2, and 3, would be pushed to newArr)
b => same thing
c => same thing

you would end up with [1,2,3,1,2,3,1,2,3]

Try using indexOf instead of equality for checking whether a value is in an array, and looping through each array separately. Also you do not really need your junk array.