題目:
使用參數(shù)過濾一個(gè)數(shù)組
destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
Passed
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
Passed
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
Passed
destroyer([2, 3, 2, 3], 2, 3) should return [].
Passed
destroyer(["tree", "hamburger", 53], "tree", 53) should return ["hamburger"].
Passed
destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan") should return [12,92,65]
代碼如下
function destroyer(arr) {
// Remove all the values
let newArr = [];
for (let i = 0; i < arr.length; i++)
{
var count = 0;//這個(gè)計(jì)數(shù)器幫助實(shí)現(xiàn)過濾
for (let j = 0; j < arguments.length; j++)
{
if (arr[i] === arguments[j])
{
count++;
}
}
if (count === 0)
{
newArr.push(arr[i]);
}
}
return newArr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);