Why doesn't this work? While Loops Exercise #10


#1


Can you create variables like so in the "do/while" code structure like I could in a "while" structure?


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


It acts as if it is an infinite loop and crashes the browser.


var count = 0;
var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("Do this once.");
      }
  while (count<1) {
      console.log("Looped once!");
      count++;
      }
};

getToDaChoppa();


#2

There are two code blocks, but only one loop. Can you guess which code block does not belong?


#3

From the instruction, I find it needless to create a count variable. You should do/while your function name really. Also, your browser would crash with your infinite while loop. Remember it is: do something while true.

See how I have rewritten to do as I perceive that you intend to.

var getToDaChoppa = function(){
  
  do {
      console.log("Do this once.");
  } while (getToDaChoppa > 1) {
      console.log("Looped once!");
  }
};

getToDaChoppa(0);

Hope this helps! :slight_smile:


#4

    do {

    } while ( condition );

There is no code block after the condition in a do..while statement.


#5

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