# Intermediate Algorithm Scripting - Diff Two Arrays

Tell us what’s happening:
Near fatal construction Accident has me learning to code at 50.
Award winning carpenter/artist
super noob coder… why am I still getting an empty Array?

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

function compareTwo(first,second){
first.forEach(number => {
if(second.indexOf(number) ===-1){
newArr.push(number)
}
});
compareTwo(arr1,arr2);
compareTwo(arr2,arr1);
}

return newArr;
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36`

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

You have defined the function `compareTwo` but you never actually call it inside the `diffArray` function. Which is probably a good thing because it will create an infinite recursion.

I screwed it up that bad. lol … that’s how I’ve always learned.
Ok, let me try to digest …
Thank You Smooth

I like this answer much better, but I would like to understand where the infinite recursion mistake happened in my previous attempt, so I can better recognize such mistakes.

function diffArray(arr1, arr2) {
{
return arr1
.concat(arr2)
.filter(num => !arr1.includes(num) || !arr2.includes(num));
}

}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

``````function compareTwo(first, second) {
first.forEach(number => {
if (second.indexOf(number) === -1) {
newArr.push(number)
}
});
compareTwo(arr1, arr2);
compareTwo(arr2, arr1);
}
``````

At some point a recursive function has to return an actual value instead of calling itself. I don’t see where that is happening in this function.

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