Diff Two Arrays
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
Here are some helpful links:
- Array.prototype.slice()
- Array.prototype.filter()
- Array.prototype.indexOf()
- Array.prototype.concat()
- for loop
- array.includes
function diffArray(arr1, arr2) {
var newArr = [];
function search(arr1, arr2) {
for (var i = 0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) === -1) {
newArr.push(arr1[i]);
}
}
}
search(arr1, arr2);
search(arr2, arr1);
// console.log(newArr);
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]); //should return ["pink wool"]