【发布时间】:2016-01-08 12:09:51
【问题描述】:
有一个初始数组(销毁函数中的第一个参数),后跟一个或多个参数。从初始数组中删除与这些参数具有相同值的所有元素。
这是我的代码,但我无法解决问题。
function destroyer(arr) {
// First I have converted the whole input into an array including the arguments
var args = Array.prototype.slice.call(arguments);
var abc=args.splice(0,1);//Here I have taken out the array part i.e[1,2,3,1,2,3] and also now args contain 2,3 only
function des(abc){
for(var i=0;i<abc.length;i++){//I tried to check it for whole length of abc array
if(args.indexOf(abc)===-1){
return true; //This should return elements of [1,2,3,1,2,3] which are not equal to 2,3 i.e [1,1] but for string inputs it is working correctly.How to do for these numbers?
}
}
}
return arr.filter(des); //but this filter function is returning empty.How to solve my problem??
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
对于destroyer(["tree", "hamburger", 53], "tree", 53),代码给出输出["hamburger"],工作正常。
但是对于驱逐舰([1, 2, 3, 1, 2, 3], 2, 3);代码没有输出。
【问题讨论】:
-
我已经回答了这个关于驱逐舰功能的问题,但我现在找不到答案,请查看stackoverflow.com/questions/33274362/…
-
这是某种测试还是测验,因为这是关于相同功能的另一个问题? stackoverflow.com/questions/33335130/…
标签: javascript arguments indexof destroy splice