I can`t understand what is wrong with this. Please help


#1

It keeps freezing my computer. Could you help me, please.
I am using the same example from: 9-The 'do'/ 'while' loop, but it is not working.

var getToDaChoppa = function(){
// Write your do/while loop here!
do {
console.log ("Trying it again" + getTodaChoppa + "!");
}while (getTodaChoppa);
};

getToDaChoppa();

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



Replace this line with your code.


#2

Hi! I figured it out myself. :slight_smile:

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

var getToDaChoppa = function(){
// Write your do/while loop here!
var condition = false;
do {
console.log ("Trying again");
}while (condition);

};
getToDaChoppa();


I still would like to understand why can`t I use this way (it is an example from:9-The 'do'/ 'while' loop): console.log ("Trying it again" + getTodaChoppa + "!");

Thank you!


#3

getTodaChoppa is undefined. This will raise an exception.

In the second example,

var condition = false;
  do {
  console.log ("Trying again");
  }while (condition);

condition is defined with a value that does not meet the while condtion, so the loop ends. The given code will be an infinite loop if condition happens to be true since there is nothing inside the loop to change it. Scary programming.

var condition;                              // declaration
do {
    condition = false;                      // definition
    console.log("Trying again... Not!");
} while (condition);

That is one of the beauties of do..while, we can hand undefined variables into the loop, and define them as needs be, inside the loop. This will take on more meaning as you progress. In the meantime, take every precaution to think out your loops before you run them, and save your work first.


#4

Thank you so much! It does make sense now :slight_smile:


#5

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