Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Stellah Avanthi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Stellah Avanthi
Front End Web Development Techdegree Graduate 18,685 Points

Just wondering even after adding the if condition for this do while loop it still throws an error, can anyone help.

Just wondering even after adding the if condition for this do while loop it still throws an error, can anyone help.

script.js
var secret = "sesame";
var correctGuess = false;

do {
 prompt("What is the secret password?");
  if (parseInt(secret) === "sesame") { 
        correctGuess = true;
    document.write("You guess Right!!!");
  }
while (parseInt(secret) !== "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>

1 Answer

You have added a lot of unnecessary code to solve challenge but it will pass with the following tips

  • Don't initialize secret with the password.
  • Update secret inside the loop by assigning it the value from the prompt
  • Do not try to convert secret to an integer. The value from prompt and what you are comparing it to are both strings
  • You don't need to write anything inside the loop
  • Since you created the correctGuess variable your condition to run could be while correctGuess is false

So like I said you can do it that way and pass the challenge but after this see if you can do it without the if statement and correctGuess variable