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 Tracking Data Using Objects The Student Record Search Challenge Solution

Loop won't quit upon clicking cancel.

I can't get this loop to quit when I click cancel on the prompt box. I'm using the code from the video:

while (true) { search = prompt('Enter your name to view your record.'); if (search === null || search.toLowerCase() === 'quit') { break; } }

What am i missing?

3 Answers

Hey James,

Your code does work. I made sure by typing it in just in case I was going crazy, but it does break out of the prompt after hitting cancel because clicking cancel returns null. Perhaps there is more going on than just the code you have posted? If you're still having problems, please post your full code. Please wrap your code in backticks like you see in the image below or in the Markdown Cheatsheet:

code

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

This is great! Although, I think a lot of people also don't know that you need at least two full carriage returns AFTER any typing for this to work.

You're right, and you need two carriage returns before the starting block of code. I didn't make this gif, though. I just utilize it! haha

Dylan Cairns
Dylan Cairns
11,191 Points

That code should work. Make sure you have a global variable named search at the top of your javascript file too. You may have a typo or case error. Variables are case sensitive.

Works for me just fine.

while (true) {
  search = prompt('Enter your name to view your record.');

  if (search === null || search.toLowerCase() === 'quit') {
    break;
  }
}