Aspects of Diff Two Arrays solution = confusing

Tell us what’s happening:
Describe your issue in detail here.

I’m having an issue understanding how this solution works exactly…

if(b.indexOf(a[i])=== -1)

This aspect of the solution is confusing me, it seems -1 identifies the desired element but I don’t get why.

  **Your code so far**

function diffArray(arr1, arr2) {
const newArr = [];
function abSwapper (a, b) {
  for(let i = 0; i < a.length; i++) {
    if(b.indexOf(a[i])=== -1) {
      newArr.push(a[i])
    }
  }
}
abSwapper(arr1, arr2);
abSwapper(arr2, arr1);
return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36

Challenge: Diff Two Arrays

Link to the challenge:

Well, lets take a look at indexOf():

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

What does this page say a return value of -1 means?

Thank you! That helps a lot! It’s all so foreign to me still that it’s sometimes difficult to know what to exactly look up. Thanks again :slight_smile:

1 Like