How to end the loop in the case of the right answer


#1


https://www.codecademy.com/courses/javascript-beginner-en-x9DnD/0/1?curriculum_id=506324b3a7dffd00020bf661#

The scrip is basically working. But how to end the loop if someone types the write answer (after the phrase "You WON"?


Replace this line with your code. 
//Check if the user is ready to play!
confirm("Ready to play?");
var UserName = prompt("What is Your Name?", " ");
if (UserName != null) {
    alert(UserName+", let's go!");
}
var answer = prompt("how much is 2+2?");
if (answer==4) {
    alert("You WON");
}
else
{alert ("second try")};
var answer = prompt("how much is 2+2?");
if (answer==4) {alert("You WON");
}
else
{alert ("last try")};
var answer = prompt("how much is 2+2?");
if (answer==4) {alert("You WON");
}
else
{
    alert ("LOST")};


#2

if they typed that prase? You could use a do while loop:

do {
  // everything that needs to be looped
} while (answer != "You WON")

this will loop until the right answer is entered


#3

If you want a cycle to end, you can type

break;

The break statement can be used to jump out of a loop.
The break statement breaks the loop and continues executing the code after the loop (if any).


#4

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