Trouble with Do/While


#1

Can someone tell me why this code causes an infinite loop?

If I remove the "do" part it works fine and loops three times, as soon as I put the DO element in it crashes

var jumps = 1

var getToDaChoppa = function(){

do {
  console.log("I'll be back");

}
while(jumps<3){
console.log("You missed the jump");
jumps++;
}
};

getToDaChoppa();


#2

A Do/While statement should look like this:

var hopCount = 1;
do {
    console.log("Hop!");
    hopCount++;
} while (hopCount < 3)

Basically this is telling the console to log "Hop!" as long as (while) the hopCount is below 3. Each time it logs "Hop!" it adds 1 to hopCount. In total, it should log "Hop!" twice.

In a do/while loop, there shouldn't be a code block after while. Only if you're making just a while statement.


#3

Ahhhhh, I understand now. It's not to DO something completely different before running the loop, the do statement is the loop, using do just makes sure it definitely does the loop once before ending.

Thanks for your help, much clearer understanding now.