Why I have to call the function twice

Tell us what’s happening:
I understand how the function works but I don’t understand why I have to call it twice
“onlyInFirst(arr1,arr2) , onlyInFirst(arr2,arr1)”

Your code so far


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

console.log(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/85.0.4183.121 Safari/537.36.

Challenge: Diff Two Arrays

Link to the challenge:

Comment out one of the function calls and see the result of diffArray(..) in the console. See if you will understand it by relating it to the if statement- after all you wrote it.

1 Like

I made a diagram to explain it as I see it. You see two balls. The yellow part is when you call onlyInFirst(arr1, arr2). The red part is when you call onlyInFirst(arr2, arr1). Your return value is adding both together.

The orange part is the part you don’t want.

https://tinyurl.com/y66bucof

1 Like

thanks bro it helped me

thanks bro It helped me