Intermediate Algorithm Scripting - Diff Two Arrays

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

Can anyone explain me this code i mean i dont understand why the function is called twice here with changing the arguments.
onlyInFirst(arr1, arr2);
onlyInFirst(arr2, arr1);

Your code so far

function diffArray(arr1, arr2) {
  const newArr = [];

  function onlyInFirst(first, second) {
    // Looping through an array to find elements that don't exist in another array
    for (let i = 0; i < first.length; i++) {
      if (second.indexOf(first[i]) === -1) {
        // Pushing the elements unique to first to newArr
        newArr.push(first[i]);
      }
    }
  }

  onlyInFirst(arr1, arr2);
  onlyInFirst(arr2, arr1);

  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/106.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

Link to the challenge:

I guess you didn’t write this code?

The idea of the two functions calls is that the first one finds the elements of the first array that are unique
And the second function call finds the elements in the second one that are unique.

I am assuming this is working code so it needs to be blurred to avoid spoiling the solution for others.

hello thanks for responding…yes i didn’t write this code i copied it from solutions…my doubt here is (second.indexOf(first[i]) === -1) why does this line is equal to -1 what does it do?

Just look up indexOf and you will see why

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