In a Javascript program I have an object with the following two (simplified) functions:
this.do_A = function() {
var nothing_changed = false;
while (!nothing_changed) {
nothing_changed = true;
for (var column=0; column<this.columns; column++) {
for (var row=0; row<this.rows; row++) {
nothing_changed = nothing_changed && this.do_B(row, column);
}
}
}
}
this.do_B = function(row, column) {
nothing_changed = true;
if (this[row][column] == null) {
nothing_changed = false;
}
return nothing_changed;
}
When running this code something very strange happens when do_B returns false, and hence nothing_changed becomes false – when reaching again the
for (var row=0; row<this.rows; row++)
line, the row variable becomes immediately this.rows and hence the inner loop terminates. Moreover, it happens in the subsequent runs of the outer loops – row is initialized to be 0, then becomes this.rows immediately and the inner loop ends again.
I have no reason what can cause this. I have tried simplifying the functions as much as possible and it keeps happening.
When
this.do_B(row, column)returnsfalse,nothing_changedwill befalse, and when it loops again and reaches
nothing_changed = nothing_changed && this.do_B(row, column), becausenothing_changedisfalse, the second expressionthis.do_B(row, column)will not be evaluated, sonothing_changedwill always befalseuntilrowreachesthis.rows.