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 trialAhmed El-Kordy
3,565 PointsThis is a bit tricky for a newbie like me, i did take my time trying to figure different ways to do it but no luck\
not sure what i did is correct or not, i really tried and took my time but i guess this is the best i can do, appreciated your feedback.
cuz i still cant pass the challange
this was my first sulotion:
let secret = prompt("What is the secret password?");
let pass = false;
do{
secret = prompt("What is the secret password?");
if (secret === "sesame"){
pass = true;
}
}while(!pass);
document.write("You know the secret password. Welcome.");
after squezing my brain cuz i really hate leaving unsolved buzzles i came up with a second one (i like this more)
let secret = prompt("What is the secret password?");
do {
secret = prompt("What is the secret password?");
if (secret === "sesame") {
}
} while (secret !== "sesame");
console.log("You know the secret password. Welcome.");
let secret = prompt("What is the secret password?");
let pass = false;
do{
secret = prompt("What is the secret password?");
if (secret === "sesame"){
pass = true;
}
}while(!pass);
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
P.S: i still cant pass the challange for some unknown reason to me so aany advice would be greatly appreciated, thank
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsYou are on the right path, I'll help by pointing you in the right direction. Your 'second one' is very close, so we will use that one to debug:
let secret = prompt("What is the secret password?");
do {
secret = prompt("What is the secret password?");
if (secret === "sesame") {
}
} while (secret !== "sesame");
console.log("You know the secret password. Welcome.");
The first thing is that you don't need to assign anything to the initial declaration of the secret
variable, so it just needs to be declared. You can just delete the assignment and leave the declaration --> let secret;
.
Second, by using let
, you aren't allowing the variable's scope to transpose into the do/while
loop. You'll need to change that to var
, so the loop has access. --> var secret;
Next, you can't (and don't need to) use an if statement
inside the loop. I see where your thinking is, but what you're trying to do using the if statement
, is actually the job of the while
block, so just delete the if block
.
Lastly, you changed the document.write
method to a console.log
method, and the challenge doesn't like that.
I feel you should be able to refactor your code now to pass the challenge. You're definitely on the right track!
Hint: There should be only 5 lines of code for this challenge.
Nice work!! :)