W3Cschool中级脚本算法(2.找出数组间差异算法挑战)

找出数组间差异算法挑战


问题:

比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素。换言之,返回两个数组的差异。

如果你被难住了,记得使用 Read-Search-Ask尝试与他人交流编程思路、但编写你自己的代码。


要求:

diff([1, 2, 3, 5], [1, 2, 3, 4, 5]) 应该返回一个数组。

["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] 应该返回 ["pink wool"]

["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] 应该返回 ["diorite", "pink wool"]

["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] 应该返回 []

[1, 2, 3, 5], [1, 2, 3, 4, 5] 应该返回 [4]

[1, "calf", 3, "piglet"], [1, "calf", 3, 4] 应该返回 ["piglet", 4]

[], ["snuffleupagus", "cookie monster", "elmo"] 应该返回 ["snuffleupagus", "cookie monster", "elmo"]

[1, "calf", 3, "piglet"], [7, "filly"] 应该返回 [1, "calf", 3, "piglet", 7, "filly"]


问题答案:


  var newArr = [];

  var arr3 = [];

  for (var i=0;i

题目链接:

https://www.w3cschool.cn/codecamp/diff-two-arrays.html

你可能感兴趣的:(W3Cschool中级脚本算法(2.找出数组间差异算法挑战))