Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,884 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,884 Points

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

Thanks.