Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Do while loop

I am using a do while loop in trying to ask the user to enter an e-mail address. I think I have the basic loop, but it doesn't know when to stop because I have not put in a specific requirement to switch the loop to false. Can someone suggest something to tell the loop that the requirements have been met? Here is the code:

var email
var correctEmail
do{
email= prompt("I want to keep in touch with you.  What is your email?");
  if(email != " @....com"){
  alert("Please enter your email.");
  }

}while(! correctEmail);

//Fixed Code Presentation

//Fixed Code Presentation

Heh, I did, too, at the same time. :D

1 Answer

A couple things are missing from your code but otherwise you are almost there. Your variables are missing semi-colons at the end and you'll need to add an else in your condition to make the correctEmail true; View below.

var email;
var correctEmail;
do{
    email= prompt("I want to keep in touch with you.  What is your email?");
    if(email != " @....com"){
        alert("Please enter your email.");
    } else {
        correctEmail = true;
        alert("Awesome! We'll be in touch soon.");
    }

}while(! correctEmail);

i hope this helps.