# Intermediate Algorithm Scripting: Diff Two Arrays

Intermediate Algorithm Scripting: Diff Two Arrays
0

#1

Hello there,

I’ve been stuck for some hours with this challenge, and even though I’ve come to a solution, the code still shows as invalid.
I’ve tried it outside of the course’s console and it seems to work fine, so I was wondering if I did something wrong.

``````function diffArray(arr1, arr2) {
var newArr = [];
// Same, same; but different.
var sortArr = arr1.concat(arr2).sort();
var counts = {};
for (var i = 0; i < sortArr.length; i++){
var num = sortArr[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
for(var item in counts){
if (counts[item] == 1){
newArr.push(item);
}
}
return newArr;
}

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

Getting the following errors:
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return `[4]` .
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return `["piglet", 4]` .
`[1, "calf", 3, "piglet"], [7, "filly"]` should return `[1, "calf", 3, "piglet", 7, "filly"]`

My first guess was that the tutorial was being picky for not returning the array in the same order, but straight from the exercise notes it reads: You can return the array with its elements in any order.

Any input is welcome.

Thanks

#2

Just looking at this first test, I see that you are returning `['4']` instead of `[4]`.

#3

``````for(var item in counts){ // item will be a string, because keys in objects are strings.