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 trialVicki Corich
6,632 PointsI suck at JavaScript- I can't pass a simple challenge
I really don't understand. I know I need a way out of the while loop but I can't figure it out.
var secret = prompt("What is the secret password?");
var sesame;
var attempts;
while ( secret !== sesame){
return prompt("What is the secret password?");
attempts = sesame;
}
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
4 Answers
Leyton Parker
6,556 Pointsin your function the you need to add 'sesame' to the secret variable like this
secret += sesame;
you also don't need the variable attempts.
Jacob Mishkin
23,118 PointsVicki, you are close on this one but there are a couple of things that need to be cleaned up here take a look at the code below and let's break it down:
var secret;
while(secret !== 'sesame'){
secret = prompt("What is the secret password?");
};
document.write("You know the secret password. Welcome.");
First, The challenges here are very strict on what they want, so if you add more to the challenge it won't pass. You have the right idea of putting the prompt inside the while loop, this will make the prompt iterate until the string "sesame" is used. You also have the right idea in having an empty string outside of the loop, var secret, and lastly you used the correct operator, !== as the condition for the loop. the strict NOT operator. First thing you need to do is make sesame a string inside the condition, if not the user cannot enter the string and the loop will never be true. the other issues that need to be removed is the return statement, remember you are not returning a value, you are looping until the condition is true. You also need to add the prompt function to the empty global variable secret inside the while loop, and lastly for the sake of the challenge you need to remove the attempts variable from the loop. Also from your code the attempts should not equal the var secret, remember that is the variable you are using to hold the prompt function in.
I hope this helps.
Gunhoo Yoon
5,027 PointsYou don't suck at JavaScript, you are just in a wrong track, I highly suggest you go back and absorb JavaScript basic again. I'm saying this because right now you don't seem to understand relationship between variable and value and what return statement is for.
This is one way you can try to solve the problem.
Break the problem and solve it from there.
Goal: Ask user for a secret and keep asking until they get it. Finally, let user know if they passed.
Have a secret.
Ask user and store response.
Check if secret matches with user response.
Keep asking until user gets the secret.
When user got the secret right, display something.
// 1. Defined variable named secret and set it to "sesame"
var secret = "sesame";
//2. Using prompt will do asking part.
// I just need to store what it got for me in a variable.
var question = prompt("What is the secret password?");
//3. while statement has condition check and
// it will keep doing something until condition is no longer met.
while (question !== secret) {
//4. Since my question has already been answered I need to ask again.
question = prompt("Try again, what is the secret password?");
}
//At this point user must have typed correct secret.
//5. document.write() will do writing on the web part for me, I just need to use it.
document.write("You got in");
Vicki Corich
6,632 PointsThanks for the feedback everyone. This is what passed. var secret = prompt("What is the secret password?"); This was provided. while (secret !=='sesame') { secret = prompt("What is the secret password?"); I tried this version but it didn't work. When the condition is true the prompt needs to be displayed. The condition runs until it is false and then the loop ends when secret = sesame. secret += sesame; I needed this, I didn't add it because it wasn't a number. Silly me! } document. write('You got in.'); I had this part correct.