Intro to While Loops 11/11


#1

why does it keep printing out 2 at the end? Also, how does my code work when it is so different from the examples?


var team = 1

for(;team <2;team++){
    console.log("my name is jason");
}

do {
    console.log("my name is Jason");
    team++
    }
    while(team <3)
    ;


#2

Your for loop prints out once, since the third execution statement in your for loop syntax added 1 to your team variable, making it 2. This reached the second condition statement, which exited the for loop after printing once.

Your do/while loop also prints out once, since your execution block also added 1 to your team variable, making it 3. This reached the condition statement, thus exiting the do/while loop after printing once.

Therefore, your program printed out twice.

Also, make sure to put a semi-colon after the declaration of your team variable, and after the "team++" statement in your do/while loop's execution block.

Let me know if this helps!


#3

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