Reputation: 134
I am hoping someone can explain what is wrong with my for loop. If I type out the for loop as if(arguments[2]){ stable = rere(stable, arguments[2]);} it works as intended. I could have more than 6 arguments and would like to understand why this loop isn't functioning properly.
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]);
Upvotes: 0
Views: 40
Reputation: 2870
You're reusing the i
variable by not declaring it in rere()
, so it gets reset on each iteration of the problem loop.
To fix it, properly declare i
inside rere()
using let
or var
.
Upvotes: 1