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

Is there anything wrong with this 'Do...While' code?

So... I'm just curious if anything is wrong with this code. My goal is to create a never ending loop until the users response is 'grayson'

here is my code so far:

var password = 'grayson';
var response;

do {
response = prompt('Who\s a sweet boy?');
  return response;
}
while (response !== password)
  document.write('welcome, brother.');

1 Answer

Steven Parker
Steven Parker
243,658 Points

A "return" statement can only be used in a function, but I don't see a function here. If you remove that line, this code should do what you want.

Also, I'm guessing you meant to use that backslash to escape an apostrophe, but the apostrophe is missing.

What is the return statement used for? I'm not comprehending it's purpose.

Steven Parker
Steven Parker
243,658 Points

Inside a function, the "return" statement causes the function to end, and optionally passes back a value to code which called the function. This will be covered in more detail as you progress in the courses.