Do/while loop crashing


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/2/3?curriculum_id=506324b3a7dffd00020bf661

I think I must be running an infinite loop because it keeps crashing, but I can't figure out why.


var bigButtsInThisCafe = 4;

var getToDaChoppa = function(){
  do {
      console.log("Are there too many big butts in this cafe?");
  } while(bigButtsInThisCafe < 6) {
          console.log("Nope! Just enough!");
          bigButtsInThisCafe++;
      }    
};

getToDaChoppa();


#2

OK. I changed it to look like this and now it's working, but I still don't understand why:

var bigButtsInThisCafe = 4;

var getToDaChoppa = function(){
do {
console.log("Are there too many big butts in this cafe?");
} while(bigButtsInThisCafe > 6) {
console.log("Never too many big butts!");
bigButtsInThisCafe++;
}
};

getToDaChoppa();


#3

Not really sure why you have a set of {} after your while. The proper syntax is do{this}while(this). Also you want to return WHILE bigButtsInThisCafe is LESS THAN 6, otherwise when it increases over and over, it will never stop. If you want to correctly write that loop, use:

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do{
     console.log("Are there too many big butts in this cafe?");
     console.log("Never too many big butts!");
     bigButtsInThisCafe++
  }while(bigButtsInThisCafe < 6);
};

getToDaChoppa();

#4

Thank you! Your syntax correction is particularly helpful.

This works, though it is returning each string twice, i.e.

Are there too many big butts in this cafe?
Never too many big butts!
Are there too many big butts in this cafe?
Never too many big butts!

Any idea why it returns everything twice?


#5

Because you're printing each line every time the loop runs through.


#6

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