Solution does not work unless function gets called twice in a row

Tell us what’s happening:
I got stuck in this problem so I peeked at the solution and almost wrote the same approach as the first solution, however for some reason it only works when I call the function twice while switching the parameters location in the function call. I would love if someone can explain to me why is this not working.
It only works when I call the function twice in a row for somereason, would love some explanation to why it did not work

Describe your issue in detail here.

  **Your code so far**

function diffArray(arr1, arr2) {
const newArr = [];
function newTry(first, second){
  for (let i=0; i < first.length; i++ ){
    if(second.indexOf(first[i]) === -1){

      newArr.push(first[i])
                      console.log(newArr)

    }
  }
}
newTry(arr1, arr2)
newTry(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/98.0.4758.102 Safari/537.36

Challenge: Diff Two Arrays

Link to the challenge:

Welcome to the forum.

I’m having a hard time understanding what you are asking here. I think you are asking, “Why do I need to call newTry twice (with switched parameters) and it won’t work if I call it only once?”

If that is your question, then it is because newTry checks if there are any elements in the first array that are not in the second. If you only did it with arr1 as the first array, it would only push on elements that arr1 has that arr2 doesn’t. But of course you could also have elements in arr2 that arr1 doesn’t have. Or both or neither of those could be true.

If that isn’t the question you are asking, please be more specific, with code examples and fewer pronouns.

1 Like

thanks man, I get your explanation, appreciate it. However what’s the issue with using pronouns , I don’t get it !

Nothing, especially. But if people use “it” too much or in ambiguous situations, it can be difficult to understand.

1 Like

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