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: