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

Subramanian K
Subramanian K
3,103 Points

Parse error

Below is my code while creating the do which loop and i got a error stating there is an syntax error : Parse error. I don't know where it went wrong....

script.js
var secretAnswer = gettingPassword();
var correctGuess = 123;
var booleanGuess = false;

function gettingPassword()
{
  var secret = parseInt(prompt("What is the secret password?"));
  return secret;
}

do
{
 secret = prompt("What is the secret password?"); 
 if( secretAnswer === correctGuess)
 {
   booleanGuess = true;
 }  
while ( ! booleanGuess ) 
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>

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Try this:

var secret;

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

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

I hope this will help you :-)

Subramanian K
Subramanian K
3,103 Points

Thank you so much. It works ....:)

Steven Parker
Steven Parker
231,399 Points

:point_right: You are missing a closing brace for your do...while loop.

The open brace following the do should be matched by a closing brace right before the while. This would be in addition to the closing brace of the if block which is there now.

However, even when repaired, this code needs some additional work to pass the challenge:

  • the correct answer must be "sesame"
  • the gettingPassword routine should be removed
  • the secretAnswer can be initialized with an empty string ("")
  • the return value of the prompt should be assigned to secretAnswer (not secret)

Henrik's condensed version would also pass the challenge.

Subramanian K
Subramanian K
3,103 Points

Thank you so much. I understand the error. Thanks for your time taken to explain me this stuff. I really appreciate that. :)