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
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsDifficulty with the code challenge
This is a code challenge for the 'do......while' loop. I am having difficulty prgressing.
This is the code we used in the last code challenge. After learning about do...while loops, don't you think this would work better in the do...while style? Re-write the code to use a do...while loop.
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.");
This is all I could come up with.
var secret = "sesame";
do {
prompt("What is the secret password?");
} while (secret === "sesame")
document.write("You know the secret password.Welcome.");
Please help. Thanks
2 Answers
Osaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsHi Samuel, in your do-while loop you set the value of the 'secret' variable to the string 'sesame'. Which is wrong, I believe the 'secret' variable is where the user input(i.e. the answer to your question "What is the secret password?") from the 'prompt' method is to be stored. You don't need to write the 'secret' variable twice(outside the loop and inside the loop), you just need to write it once within the loop. Lastly the test condition for your do-while loop is meant to be the opposite(secret !== 'sesame'), this way when the 'secret' variable does equal the string 'sesame' - the loop will stop.
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsNo no, it's not what I mean. In these situation he makes the first condition true ...blah blah.. ok take for example. the sesame example.
var secret;
do {
secret = prompt("What is the secret password?");
} while (secret !== "sesame")
document.write("You know the secret password. Welcome.");
Although the answer is sesame, the while statement says secret is !== sesame. somehow in my mind it makes sense that secret === sesame for the code to evaluate to true, but in this case because the answer is sesame, he needs the code to evaluate to false so he changes secret to !== sesame. In other times when we used comparism operators we usual checked to see if the user's response matches the correct answer, so usually the initial value would be false then if it matches then it would change to true ie === . .I don't know if I'm making any sense to you here man.
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsHi Osaro Igbinovia that worked. Thank you. But I'm still very confused as to why the strict not equal sign is the only thing that works. Someone told me it's just how it is set up in JS but I still don't fully understand it. Anyway thanks for your help bro.
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsHi Osaro Igbinovia When I wrote the code the way you suggested, it worked fine in my personal text editor program, but for the challenge they said, Bummer "You should declare the
secretvariable before the loop. Otherwise, you re-create that variable each time through the loop." so i had to create a var secret; at the top then put the secret inside of the loop. like so. But anyway, you helped me big time. Thanks bro.still hopelessly stuck on why we use the strict not equal.
Osaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsOsaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsGlad I could be of help Samuel, as regards the use of 'strict not equal' (!==) and not just simply 'not equal' (!=) is because 'strict not equal' compares both the value type and value of the two JavaScript elements in the test condition. Whereas 'not equal' only compares the value.