I tried rewriting the same loop for both a for loop and a while loop. The for loop will print my lines to the screen, but the while loop does not. Why would they not work the same? What would I need to do to the while loop to make it have the same behavior as the for loop?
var write_this = '\
';
var i = 0;
while (i < 3) {
write_this += 'Does not print to console three times.\
';
i++;
}
var write_this = '\
';
for (i = 0; i < 3; i++) {
write_this += 'Prints to console three times.\
';
}
Well, there is no actual script. I’m just playing with the different loops. I typed the first loop into Firebug and ran it exactly as I listed it above, and the Firebug console just printed this:
>>> var write_this = ’
'; var i = 0; while (i < 3) … not print to console three times.
'; i++; }
2
(Not sure where the “2” is coming from)
Then I clear the while loop and run the for loop all by itself and Firebug gives me:
>>> var write_this = ’
'; for (i = 0; i < 3; i++) {…te_this += 'Prints to console three times.
'; }
"
Prints to console three times.
Prints to console three times.
Prints to console three times.
"
What I’m trying to figure out is why the while loop won’t print the 3 lines like the for loop does. I’m guessing it must be printing them out somewhere if it’s working for you, but I wonder why my Firebug is acting differently?