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 A Closer Look At Loop Conditions

Java script loop challenge

var secret = prompt("What is the secret password?");

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

while (secret != "sesame"{ secret = prompt ("what is the secret password?"); attempts +=1; }

It says bummer . why ? also i don't know why i put something like " attempts +=1 because i have not defined an attempts variable but i thought you should because its a loop but don't understand

app.js
var secret = prompt("What is the secret password?");

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

while (secret != "sesame"{
  secret = prompt ("what is the secret password?");
attempts +=1; 
  }
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>

2 Answers

Tom Byers
Tom Byers
13,005 Points

At least part of the problem is in this section of code. You are missing a closing parentheses after "sesame". It should look like this:

while (secret != "sesame") {
  secret = prompt("what is the secret password?");
  attempts +=1; 
  }

Does that work?

Hey many thanks it works. But what i dont understand is why we have to put attempts+=1 . attempts is not defined even as a variable anywhere in the script...how did we pluck it from no where ...is attempts a variable...what is it ?

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

deepsabey

The question doesn't ask you to use attempts variable.

You don't have to put attempts += 1 nor use it unless you want to use it as a naive break point for blocking further login. If you want to use it you need assign it before while loop start as var attempts = 0.

Tom Byers
Tom Byers
13,005 Points

Yeah it should be defined somewhere. It's not doing anything in that section of code as it is. But maybe later in the challenge you recall how many attempts the user took to login? But you're right - it's arbitrary as it is.

Many thanks Tom Byers and Gunhoo Yoon