Diff Two Arrays using .forEach instead of for loop

Diff Two Arrays using .forEach instead of for loop
0

#1

Tell us what’s happening:
// I’m not sure why using .forEach would give me a different answer than if I were to use a for loop. Can anyone explain?

Your code so far

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  function compare(first, second)  {
    first.forEach(function(item)  {
      if(second.indexOf(item === -1)){
        newArr.push(item);
      }
    });
  }
  compare(arr1, arr2);
  compare(arr2, arr1);
  
  return newArr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

You have a closing parentheses misplaced in the following line:

if(second.indexOf(item === -1)){

#3

blast, you’re right, ha! Thanks for your second pair of eyes!