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 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!