Can someone please explain why my code is returning an empty array

Tell us what’s happening:
Can someone please explain why my code is returning an empty array.

  **Your code so far**

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

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

User Agent is: Mozilla/5.0 (X11; CrOS aarch64 13816.82.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.218 Safari/537.36

Challenge: Diff Two Arrays

Link to the challenge:

Hello Rnwego.

Your function is return an empty array because your if conditional is never met. In the example given by this challenge, arr2 contains every element that arr1 has. So there will never be a point where arr2.indexOf(arr1[i] === -1). Even if arr1 contained elements that arr2 did not, your function would still miss any element in arr2 that arr1 did not contain.

You’ll need to find a way to see the unique elements in both arrays instead of just one of them.

1 Like

thank you so much for your help

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