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
12,825 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!