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

Michael Elmallakh
Michael Elmallakh
2,531 Points

My code runs only when I write quit

Please I want your help as the code runs only when I write quit

var instock = [ 'apple', 'Egg', 'milk', 'cheese', 'Carrot', 'Potato', 'Bread'];
var search;

function print(message){
    document.write('<p>' + message + '</p>');
}

while (true) {
    search = prompt("search for a product in a store. Type 'list' to show all the product or 'quit' to exit");
    search = search.toLowerCase();
    if ( search === 'quit') {
        break;
    } else if ( search === 'list') {
        print( instock.join(', '));
    } else {
        if ( instock.indexOf(search > -1)) {
                print('Yes, we have ' + search + ' in the stock');
        } else{
            print(search + 'is not in the stock');
        }
    }
}

8 Answers

Ayush Desai
Ayush Desai
1,386 Points

This video was shot some time ago so now many browsers including your own only run all of your code when you type quit

David Bath
David Bath
25,940 Points

Well, in your list "Egg", "Carrot", "Potato", and "Bread" are all capitalized, so your lower-cased search term will never match those items! I don't see why the other ones wouldn't work though.

Michael Elmallakh
Michael Elmallakh
2,531 Points

I don't know any way thanks Dav

Assuming you are using chrome, I think it is just the way that chrome executes the code. If you try it out in Firefox it should print to the page while the loop is still running.

Michael Elmallakh
Michael Elmallakh
2,531 Points

its working on firefox but if I write any thing it shows me that it appears on the stock :D

gary peart
gary peart
6,496 Points

Hi Michael,

I've looked at your code.

As pointed out by David above, you have used uppercase on the first letter of some of your array items, then lowercase on the first letter of other items. This will affect the search variable's value matching some of the items in your array.

I've tested your code and the issue regarding your last comment is caused by:

if ( instock.indexOf(search > -1)){

You almost have that line correct. Move the ) to the left and close the indexOf method before the > operator as below:

if ( instock.indexOf(search) > -1){

Hope this helps, if you've not resolved the issue yourself already ;o)

Dennis Amiel Domingo
Dennis Amiel Domingo
17,057 Points

Hi, Michael.

When you open the Workspace for this video, you'll notice that Dave posted a comment about this problem:

"/* Important note The behavior of most browsers has changed since this video was shot, so you won't see the same thing as I demonstrate in the video. In the video, you'll see that my script is able to print out to the browser using document.write( ) while inside a loop.

Most browsers no longer do that: they wait until the loop finishes and then they print to the window. So, you'll see a blank page until you type quit in the prompt window — then you'll see all the output printed to the screen.

Sorry for the confusion, and we'll update the video soon. */"

Now, to solve this problem, replace all instances of "print" with "alert" and you'll see your list as well as the other messages.

Example:

From

print( instock.join(', '));

To

alert( instock.join(', '));
Anastasia Savina
Anastasia Savina
6,165 Points

Hi there! May be this way is not good, but if add break after each condition (if, else if and else) code is working well :)

Yan B
Yan B
2,785 Points

I used break and it works.