Why does my code print out the extra number? (7.11)


#1



Hi, all!
The terminal continues to print:

I'm printed 5 times!
I'm printed 5 times!
I'm printed 5 times!
I'm printed 5 times!
I'm printed 5 times!
I'm printed twice!
I'm printed twice!
1

My question is, why does it print the 1? I realized that if I shift the position of myMoney++; up by one line in the "do" section of my code the error goes away. Why is this?

// Write your code below!
var myMoney = 0

for(i=0; i<10; i+=2) {
    console.log("I'm printed 5 times!");
};
do {
    console.log("I'm printed twice!");
    myMoney++;
}   while (myMoney<2)


#2

it seems to be console behaviour, by moving it up one line, a variable is not updated after the last console.log(), so the console logs nothing extra.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.