function diff(arr1, arr2) {
var newArr = [];
// Same, same; but different.
// first, define function for filtering arrays
// to trim arrays to unique values
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function valueHere(value, index, self) {
return self.indexOf(value) === index;
}
arr1 = arr1.filter( onlyUnique );
arr2 = arr2.filter( onlyUnique );
// if they're different lengths, any values not common to both will be in longer array
if (arr1.length > arr2.length) {
var longerArr = arr1;
var shorterArr = arr2;
} else {
var longerArr = arr2;
var shorterArr = arr1;
}
// pick off the first item in the longer array and look for that value in the shorter array
var currentItem = longerArr[0];
var remainingList = longerArr.slice(1);
if (shorterArr.indexOf(currentItem) === -1) {
newArr.push(currentItem);
} else {
// trim a common element from the shorter Array, too.
shorterArr.splice(shorterArr.indexOf(currentItem), 1);
if(remainingList.length > 0) {
//recursion!
return diff(remainingList, shorterArr);
} else {
return newArr;
}
}
}
//
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);