Intermediate Algorithm Scripting - Diff Two Arrays

Should i have !== -1 set to <=0 or is something else the issue? maybe the way i coded the indexOf part of it?

Your code so far

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

Link to the challenge:

What does this mean?

Each side of the && needs to be an independent statement.

arent arr1 and arr2 already defined since theyre used as arguments?

would i just want to return arr1 and arr2 before looping then? or do something like let a1 = arr1.slice() and same for arr2?

arr1 and arr2 are defined. a and newArr are defined. arr is not.

You should really, really read this.

Ok i see it now haha :slight_smile: Thanks, totally missed it

if(arr1.indexOf(a[i]) === -1 && arr2.indexOf(a[i]) === -1 ) Ok so like this but with different equality operators right?

Right, you need two separate comparisons, but you need to make sure the comparisons are expressing the logic you need.

I thought === -1 is the same as saying if one of the iterations is not present in arr1/arr2

or setting it equal to < 0 to specify it as not in the array

Right, so this says "a[i] is not in arr1 and a[i] is not in arr2.

Is that what you want?

No because it has to be in one but not the other so i can identify the missing element and solve it

Right, so you probably need a different logical combination of those two conditions.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.