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: