--- id: a5de63ebea8dbee56860f4f2 title: Diff Two Arrays isRequired: true challengeType: 5 videoUrl: '' localeTitle: 差分两个阵列 --- ## Description
比较两个数组并返回一个新数组,其中只有在两个给定数组中的一个中找到的任何项,但不能同时返回两个数组。换句话说,返回两个数组的对称差异。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。 注意
您可以按任何顺序返回包含其元素的数组。
## Instructions
## Tests
```yml tests: - text: 'diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])应该返回一个数组。' testString: 'assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === "object", "diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) should return an array.");' - text: '["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回["pink wool"] 。' testString: 'assert.sameMembers(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]), ["pink wool"], "["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["pink wool"].");' - text: '["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回一个包含一个项目的数组。' testString: 'assert(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 1, "["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with one item.");' - text: '["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回["diorite", "pink wool"] 。' testString: 'assert.sameMembers(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]), ["diorite", "pink wool"], "["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["diorite", "pink wool"].");' - text: '["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回一个数组有两个项目。' testString: 'assert(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 2, "["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with two items.");' - text: '["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]应该返回[] 。' testString: 'assert.sameMembers(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]), [], "["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return [].");' - text: '["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]应返回一个空数组。' testString: 'assert(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]).length === 0, "["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return an empty array.");' - text: '[1, 2, 3, 5], [1, 2, 3, 4, 5]应该返回[4] 。' testString: 'assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4], "[1, 2, 3, 5], [1, 2, 3, 4, 5] should return [4].");' - text: '[1, 2, 3, 5], [1, 2, 3, 4, 5]应该返回一个带有一个项目的数组。' testString: 'assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1, "[1, 2, 3, 5], [1, 2, 3, 4, 5] should return an array with one item.");' - text: '[1, "calf", 3, "piglet"], [1, "calf", 3, 4]应返回["piglet", 4] 。' testString: 'assert.sameMembers(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]), ["piglet", 4], "[1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return ["piglet", 4].");' - text: '[1, "calf", 3, "piglet"], [1, "calf", 3, 4]应该返回一个包含两个项目的数组。' testString: 'assert(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]).length === 2, "[1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return an array with two items.");' - text: '[], ["snuffleupagus", "cookie monster", "elmo"]应该返回["snuffleupagus", "cookie monster", "elmo"] 。' testString: 'assert.sameMembers(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]), ["snuffleupagus", "cookie monster", "elmo"], "[], ["snuffleupagus", "cookie monster", "elmo"] should return ["snuffleupagus", "cookie monster", "elmo"].");' - text: '[], ["snuffleupagus", "cookie monster", "elmo"]应该返回一个包含三个项目的数组。' testString: 'assert(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]).length === 3, "[], ["snuffleupagus", "cookie monster", "elmo"] should return an array with three items.");' - text: '[1, "calf", 3, "piglet"], [7, "filly"] [1, "calf", 3, "piglet", 7, "filly"] [1, "calf", 3, "piglet"], [7, "filly"]应该返回[1, "calf", 3, "piglet", 7, "filly"] 。' testString: 'assert.sameMembers(diffArray([1, "calf", 3, "piglet"], [7, "filly"]), [1, "calf", 3, "piglet", 7, "filly"], "[1, "calf", 3, "piglet"], [7, "filly"] should return [1, "calf", 3, "piglet", 7, "filly"].");' - text: '[1, "calf", 3, "piglet"], [7, "filly"]应该返回一个包含六个项目的数组。' testString: 'assert(diffArray([1, "calf", 3, "piglet"], [7, "filly"]).length === 6, "[1, "calf", 3, "piglet"], [7, "filly"] should return an array with six items.");' ```
## Challenge Seed
```js function diffArray(arr1, arr2) { var newArr = []; // Same, same; but different. return newArr; } diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); ```
## Solution
```js // solution required ```