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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Abhijit Das
Abhijit Das
5,022 Points

Hello I want to make a small application. Could I use while loop on this situation?

I want to guess a password and I have 3 attempts to do that. Every time, if i failed to guess correct password the prompt dialogue will pop up to ask it again, and an alert function will show how many attempts are left. When all attempts are finished it will show me another alert message that you loose all your attempts. I try to do it here : http://codepen.io/anon/pen/wJBEWW?editors=0010 but it's not happening. Please help.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sure you can use a while! I reworked your code a bit so that it works like I think you're expecting. Take a look:

var password = prompt("What is your password?");  //get the user's guess
var count = 1;  //we start count at 1
var attempt = 2 ;  //start attempts left at 2 because they've already made one guess
while(password !== "abhijeet" && count < 3) { // while the password was not correct
  password = prompt("Try again. You have " + attempt + " attempts left.");  //get another guess and tell the user how many attempts are left
  count += 1;  //increase the count
  attempt -= 1;  //decrease attempts left
}
if (password === "abhijeet") {  // check to see if their guess was correct
  alert("Your guess is right");  //if it was print a success message
} else {  //otherwise if no guesses were correct
  alert("Sorry. You do not know the password.");  //Print a failure message
}

Hope this helps! Let me know if you need further clarification! :sparkles:

Abhijit Das
Abhijit Das
5,022 Points

Hello Jennifer Nordell, Indeed it helps me, and I clearly understand it. THANK YOU. :D

Abhijit Das
Abhijit Das
5,022 Points

I did the same scenario, this time with do while loop....hope the approach is ok.

var attempt = 0;//attempt value initially zero//
var count = 5;//maximum attempt variable//
do {
  var password = prompt("what is your password?");//Ask user to guess//
  attempt++;//increase the attempt//
  count--;//decrease the attempt//
  if (password === "hello" && attempt === 1) {
    alert("congratulations!! You guess it right in one chance.");//message for first time correct guess//
  } else if (password === "hello") {
      alert("Your guess is right");//message for guess is right//
  }
    else if (count === 0) { //checking no attempt left//
      alert("No attempt left");//message for no attempt left//
  } else {
      alert("This was your " + attempt + " attempt you have " + count + " lefts.");//message of how many attempts are left//
  }
}
while (password != "hello" && attempt < 5);//while condition//