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

browser or the script ?

Hello All,

I created this script from the "JavaScript Loops, Arrays and Objects" and when i type list or search something in array it dosent display on the page, but when i type quit, then the all written information in prompt is displayed. Is it the script or is it my chrome browser? if it`s chrome then how can i disable that?

script.js
var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;

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

while (true) {
    search = prompt("Search for a product in our store. Type 'list' to show all of the produce and 'quit' to exit");
    search = search.toLocaleLowerCase();
    if ( search === 'quit' ){
        break;
    } else if ( search === 'list' ){
        print( inStock.join(', ') );
    } else {
        if ( inStock.indexOf( search ) > -1 ){
            print('Yes, we have '+ search + ' in the store');
        } else {
            print( search + ' is not in Stock.')
        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,968 Points

All modern browsers (not just Chrome) defer rendering to the page until after the script code has finished. Try substituting "alert(message);" for the body of the "print" function.

As you continue in your studies, you'll soon learn better ways to to make your pages interactive using forms and page controls.