curriculum/challenges/english/blocks/lab-symmetric-difference/68ad9821ee41baad9cb0fd4e.md
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.
Example:
Array A: ["diamond", "stick", "apple"]
Array B: ["stick", "emerald", "bread"]
Result: ["diamond", "apple", "emerald", "bread"]
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
diffArray should return an array.filter method.You should have a function named diffArray.
assert.isFunction(diffArray);
The diffArray function should use the filter method to filter out items that are present in both arrays.
assert(/\.filter\(/.test(diffArray.toString()));
diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]) should return ["pink wool"].
assert.deepEqual(diffArray(
["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"],
["diorite", "andesite", "grass", "dirt", "dead shrub"]
), ["pink wool"]);
diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]) should return ["diorite", "pink wool"].
assert.deepEqual(diffArray(
["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"],
["andesite", "grass", "dirt", "dead shrub"]
), ["diorite", "pink wool"]);
diffArray should return an empty array when called with two identical arrays.
assert.deepEqual(diffArray(
["andesite", "grass", "dirt", "dead shrub"],
["andesite", "grass", "dirt", "dead shrub"]
), []);
diffArray(["pen", "book"], ["book", "pencil", "notebook"]) should return ["pen", "pencil", "notebook"].
assert.deepEqual(diffArray(
["pen", "book"],
["book", "pencil", "notebook"]
), ["pen", "pencil", "notebook"]);
diffArray(["car", "bike", "bus"], ["bike", "train", "plane", "bus"]) should return ["car", "train", "plane"].
assert.deepEqual(diffArray(
["car", "bike", "bus"],
["bike", "train", "plane", "bus"]
), ["car", "train", "plane"]);
diffArray(["apple", "orange"], ["apple", "orange", "banana", "grape"]) should return ["banana", "grape"].
assert.deepEqual(diffArray(
["apple", "orange"],
["apple", "orange", "banana", "grape"]
), ["banana", "grape"]);
diffArray([], ["apple", "banana"]) should return ["apple", "banana"].
assert.deepEqual(diffArray(
[],
["apple", "banana"]
), ["apple", "banana"]);
diffArray(["apple", "banana"], []) should return ["apple", "banana"].
assert.deepEqual(diffArray(
["apple", "banana"],
[]
), ["apple", "banana"]);
diffArray([], []) should return [].
assert.deepEqual(diffArray(
[],
[]
), []);
function diffArray(arr1, arr2) {
return arr1
.filter(item => !arr2.includes(item))
.concat(arr2.filter(item => !arr1.includes(item)));
}