*****EDITED TO INCLUDE TRUE MINIMAL REPRODUCIBLE EXAMPLE*****
I have a nested for
loop that reads elements from an array and writes to another array. The inner for
loop is supposed to check elements in one of the arrays and break
back to the outer array when the elements are not equal.
I have referenced stackoverflow for all related questions to try and find a solution. I have tried writing the array elements into variables and comparing them, but the result is the same; the if
condition is triggering erratically and I cannot determine what the pattern is.
I have been using Logger.log
in my test conditions and have managed to pinpoint the for
statement that is causing the issues - but I haven't been able to find a solution.
function importCollection() {
var xpacs = [[1], [1], [1], [1], [1], [1], [2], [2], [3], [3], [3]];
var cards = [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]];
var cards0 = [];
var cards1 = [];
var cards2 = [];
var cards3 = [];
var cards4 = [];
var a = 0;
var b = 0;
var c = 0;
var x = 0;
var y = 11;
for (b;c<y;b++) {
x = xpacs[c];
// Logger.log("x: "+x)
// Logger.log("xpacs: "+xpacs[c]);
PACK_LOOP: for (var a=0;a<5;a++) {
if (c==y || x!=xpacs[c]) { // ***** ERROR - This is the code that is triggering too often
Logger.log("Row: "+b);
Logger.log("Col: "+a);
Logger.log("Card: "+c);
Logger.log(x);
Logger.log(xpacs[c]);
Logger.log("This if statement shouldn't be triggering when the two lines above are equal!");
break PACK_LOOP
}
eval("cards"+[a]+"[b] = cards[c]");
c++;
}
}
Logger.log(cards0);
Logger.log(cards1);
Logger.log(cards2);
Logger.log(cards3);
Logger.log(cards4);
}
The expected results should be:
[[1.0], [6.0], [7.0], [9.0]]
[[2.0], null, [8.0], [10.0]]
[[3.0], null, null, [11.0]]
[[4.0], null, null, null]
[[5.0], null, null, null]
Thank you to everyone for your help