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 Tracking Multiple Items with Arrays Useful Array Methods

Amy Hsieh
Amy Hsieh
6,023 Points

My code runs but a bit differently. Why?

In the video he types 'list', and then it writes right away on the screen. However, I typed 'list', no reaction. After I type quit, the array prints on the screen.

By the way I don't understand why we need a while (true) here. I know 'True' is a boolean, but what condition is 'true' here to make the while loop run?

My code is below:

while (true) {
    search = prompt("Search for a product in our store. Type 'list' to show all of the produce and 'quit' to exit");
    if (search === 'quit') {
        break;
    } else if (search === 'list') {
        print(inStock.join(', '));
    }
}

2 Answers

Robert Stamate
Robert Stamate
13,227 Points

The

true

in

while(true)

makes the while loop start when the document loads. A while(false) would not start on document load. I tested the code and it works fine, I re-watched the video and if you wish to re-test in the console what he does, change print() with alert().

You can check more here: MDN While Loop

Dehn Hunsworth
Dehn Hunsworth
7,189 Points

Not sure how long it has been there, but they put an answer to your question at the top of the search.js file linked in this workspace. Had me frustrated for a minute too!