I recently finished the diff. two arrays challenge in the intermediate scripting algorithms section of the JS certification and was wondering if my solution can be slimmed down a bit.
function check(cArr1, cArr2) {
// First, map a new array from arr1 containing values not found in arr2...
let filterArr1 = cArr1.map(
function(a) {
if (cArr2.includes(a) == false) {
return a;
}
}
// Then filter out any [""] values. These occur when the arrays are of different lengths.
).filter(a => a);
// Do the same thing in reverse...
let filterArr2 = cArr2.map(
function(a) {
if (cArr1.includes(a) == false) {
return a;
}
}
).filter(a => a);
return filterArr1.concat(filterArr2);
};
function diffArray(arr1, arr2) {
return check(arr1, arr2);
}
console.log(diffArray([1, "calf", 3, "piglet"], [7, "filly"]));