W3Cschool初级脚本算法(15.数组中任意多个值算法挑战)

数组中任意多个值算法挑战


问题:

 实现一个 destroyer 函数,第一个参数是初始数组,后跟一个或多个参数。从初始数组中删除与这些参数具有相同值的所有元素。


要求:

destroyer([1, 2, 3, 1, 2, 3], 2, 3) 应该返回 [1, 1].

destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) 应该返回 [1, 5, 1].

destroyer([3, 5, 1, 2, 2], 2, 3, 5) 应该返回 [1].

destroyer([2, 3, 2, 3], 2, 3) 应该返回 [].

destroyer(["tree", "hamburger", 53], "tree", 53) 应该返回 ["hamburger"].


问题答案:

function destroyer(arr) {
// Remove all the values

return [].slice.call(arguments).reduce(function (prev, next) {

        return prev.filter( e => e !== next);

    });
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

题目链接:

https://www.w3cschool.cn/codecamp/seek-and-destroy.html

猜你喜欢

转载自blog.csdn.net/qq_42044073/article/details/82491676