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

Can anyone please help me with this code challenge..?

Can anyone please help me with this code challenge..?

app.js
var secret = prompt("What is the secret password?");

var i = 0;

while(secret !== "sesame")
  prompt("That's not it! Try again");
  i +=1;
}

else if( secret === "sesame")
document.write("You know the secret password. Welcome.");
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>

1 Answer

Andreas Nyström
Andreas Nyström
8,887 Points

Hi. You don't need a var i in this one. All you got to do is change the if to a while loop. The while loop then checks if password !== "sesame". If it is not "sesame" the while loop continues because password NOT EQUAL TO "sesame" is true.

You then just need to write the other message outside the loop. Because if password === "sesame" then the statement is false and it jumps out of the loop.

You can also not use an else if if you dont first have an if. And you don't need that for this. This is what it should be like, try to understand what happening or ask if you have any questions. Cheers buddy!

var secret = prompt("What is the secret password?");

while(secret !== "sesame") {
  secret = prompt("That's not it! Try again");
}

document.write("You know the secret password. Welcome.");

Thanks a lot Andreas.!

Andreas Nyström
Andreas Nyström
8,887 Points

Anytime, Mike! If you see this as the solution, please mark it as "best answer".

Thanks.