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 Create a `do...while` loop

Mihia Maftei
Mihia Maftei
1,315 Points

Hi if somebody done this can show me how he done it pls i can't see what i do wrong...thx

I need to change the while loop to a do...while loop thx

script.js
// This is the code what i have to change in a do 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 the code what i tried it but still doesn't work is not hard but i can't see what is wrong ...

var secret = prompt("What is the secret password?");
var guess ;
do{
  guess = prompt("What is the secret password?");
}
while (guess === "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="script.js"></script>
</body>
</html>

4 Answers

Raffael Dettling
Raffael Dettling
32,999 Points

Hm ok i know the pain ^^ Passed the challenge with this

var guess ;
do{
  guess = prompt("What is the secret password?");
}
while (guess !== "sesame")
  document.write("You know the secret password.Welcome.");
Raffael Dettling
Raffael Dettling
32,999 Points

You will notice itΒ΄s not the last time you will struggle while programming :) Sometimes you just need a break donΒ΄t try to force progress

Raffael Dettling
Raffael Dettling
32,999 Points

You dont need the variable "secret" and its prompt the variable "guess" does the job and it should run the loop while guess !== sesame :)

Mihia Maftei
Mihia Maftei
1,315 Points

Still the same i lost 3h in this page and i tried a lot to it but is looking like nothing is working... Did you finish it? this task?

Mihia Maftei
Mihia Maftei
1,315 Points

Nice now i can look to see what i done wrong the code is working fine i didnt know i can delet that var secret(even if i wanted to delet it still i didn't done the code right) ... you just made my day better thank you !