我希望有人能解释一下我的for循环出了什么问题。如果我输入for循环,就好像(Arguments2){ stable = rere(stable,arguments2);}它可以正常工作。我可能有6个以上的参数,我想知道为什么这个循环不能正常工作。
function sym() {
function rere(a,b){
var check = [];
for(i=a.length-1; i>=0; i--){
for(x=b.length-1; x>=0; x--){
if(a[i] == b[x]){
check.push( a.splice(i,1) );
}
}
}
for(i=b.length-1; i>=0; i--){
for(x=check.length-1; x>=0; x--){
if(b[i] == check[x][0]){
b.splice(i,1);
}
}
}
var stable = a.concat(b);
return stable;
}
var stable = rere(arguments[0], arguments[1]);
//problem is HERE. The for loop repeating rere function crashes repl.it
for(i=2; i<arguments.length; i++){
stable = rere(stable, arguments[i]);
}
//End problem.
stable = stable.filter(function(a,b,c){
return b == c.indexOf(a);
});
return stable;
}
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]); 发布于 2017-09-15 00:01:58
您没有在rere()中声明i变量,这是在重用它,因此它在问题循环的每次迭代中都会被重置。
要修复它,请在rere()中使用let或var正确声明i。
https://stackoverflow.com/questions/46223657
复制相似问题