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

Stellah Avanthi
Stellah Avanthi
Full Stack JavaScript Techdegree Student 5,934 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

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,164 Points

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