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

Barb Oladele
Barb Oladele
1,416 Points

so while parsing error

I cannot get pass the parsing error:

var secret = prompt("What is the secret password?"); int i =10; do{ (secret !== "sesame" ) ; secret = prompt("What is the secret password?"); i --1; }While (i>1); document.write("You know the secret password. Welcome.");

script.js
var secret = prompt("What is the secret password?");
 int i =10;
do{
(secret !== "sesame" ) ;
  secret = prompt("What is the secret password?"); 
   i --1;
}While (i>1);
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>
Trent Stenoien
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 Points

"int i = 10" While loops don't always need a counter if you've provided a way to break the loop; in this case 'secret !== "sesame"'. Second, keep in mind that do...while loops will run once automatically so you don't need to create the var prior to running it.

do {var secret = prompt("What is the secret password?")
} while (secret !== "sesame");

document.write("You know the secret password. Welcome.");

p.s. I also think you're using 'int' incorrectly, but I don't know enough about it to say for sure! Haha...

2 Answers

Barb Oladele
Barb Oladele
1,416 Points

I get bummer error with more explanation on the below code

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

Trent Stenoien
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 Points

Check the Markdown Cheatsheet to format your posts here; it'll make it easier to read. :)

//Your code
var secret= 0; do { prompt("What is the secret password?") secret +=1; } while (secret !== "sesame");
document.write("You know the secret password. Welcome.");

First thing I notice about your code is that you're using 'var secret' as a counter. Remember that you don't need a counter for while statements unless you want it to run a certain number of times. In this case your loop will run until secret == "sesame", but secret is a number so it will never be a string called "sesame".

Keep in mind that whenever you redefine a variable in JS the code is 'secret = __________'. So when you write prompt("What is the secret password?"), a prompt will come up, but it isn't converted into a variable to be used. Rewrite this as 'secret = prompt("What is the secret password?")'.

var secret;
//Create variable to be used later

do { 
secret = prompt("What is the secret password?") 
//Redefine secret as the user input from prompt
/*secret +=1; isn't needed here because we don't need a counter for this loop.*/} 
while (secret !== "sesame");
//Repeat while the secret is incorrect

document.write("You know the secret password. Welcome.");
//Write to document once password is guessed
Barb Oladele
Barb Oladele
1,416 Points

Thanks trent. I thought the variable had to be initialized. I will follow your pattern of commenting every line, it does make the code easy to write.

Many thanks