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

code will not run correctly, While loop

Hi,

I have spent a lot of time trying to figure out this question with no success. Why is my code not running? Also, please post the correct code with this question (I have already tried several times before and am getting very frustrated). Thank you

var sesame == true; var secret = prompt("What is the secret password?"); while (secret !== sesame) { prompt("What is the secret password?"); } document.write("You know the secret password. Welcome.");

app.js
var sesame == true;
var secret = prompt("What is the secret password?");
while (secret !== sesame) {
  prompt("What is the secret password?");
}
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>

2 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Daniel,

The idea is what you want to keep on asking the user for the password until the get it correct. In this case the password is "sesame". To make things simple, you want the string "sesame", not make a variable out of it.

If the user got it wrong, you want to ask the user again for the password and then assign that value to 'secret' so that it can be checked again in the while loop condition.

var secret = prompt("What is the secret password?");
while (secret !== "sesame") {
  secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");

Thank you Jon, your explanation coupled with a visual example made this error clear to me. It makes more sense now.

Tommy Gebru
Tommy Gebru
30,164 Points
//var question=answer
var secret = prompt("What is the secret password?"); 
//while(false)
while (secret !== "sesame") {
//loop
  secret = prompt("What is the secret password?");
}
//return the following when loop is exited
document.write("You know the secret password. Welcome.");

//Therefore the loop was exited, when the while argument changed, from false to true 

Outside the while loop assign sesame to true using only one equals sign and see if that works.