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

Julian Martinez
Julian Martinez
3,094 Points

why my code doesn't print anything until I type quit?

everything works fine, the code prints what its supposed to, but it won't display until I type quit... I would like to know why? Thank you guys.

You are going to have to post your code for us to help you, Julian. Be sure to format it like so:

code

Jaemin Yi
Jaemin Yi
4,040 Points

I'd also like to know the answer to this question. I'm following all the code that the instructor is writing EXACTLY...and yet nothing is printed until after I type "quit". Is this a browser issue (I'm using Safari) or an issue with the instructors code?)

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"); 
  if ( search === 'quit') {
    break;
  } else if ( search === 'list' ) {
    print( inStock.join(', ') );
  }
}

Hey Jaemin,

I have used Safari before, and I've noticed this behavior myself. I think you can get around this behavior by setting an element on the page to an id such as "output" and then adding on to the "innerHTML" of that element. That should update the content immediately once print() is called, if I remember correctly:

The HTML

<div id="output"></div>

The javascript

function print(message) {
  document.getElementById('output').innerHTML += message;
}
kabir k
kabir k
Courses Plus Student 18,036 Points

I have tried Google Chrome and it behaves the same with my code. My default browser is Safari, so I don't think it has anything to do with the browser. Thanks, Marcus, for the alternative way of getting to work but why do we have to "get around it" instead of just using the same code to get the same results?

I, too, use Google Chrome and using document.write() prints the code out immediately.

And my reasoning was that document.write() might work differently in Safari than using innerHTML. I can't use Safari because I use Linux. It was more of a theory to test than anything else.

3 Answers

Hey Julian,

I cleaned up your code some and fixed your issue. Your else-if statements were all over the place and now they are organized. I also added an extra "<br>" to your print function so that each statement will print on a new line and look better, but keep in mind that generally it's not a good practice to use document.write on an actual web page. Here it is:

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(message + "<br>"); 
}

while (true) { 
    search = prompt("Search for an item in our store. Type 'list' to show all products and '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 store'); 
    } 
    else { 
        print(search + ' item is not in stock'); 
    }
}
Julian Martinez
Julian Martinez
3,094 Points

Thank you very much!!!! I've been learning From-End development for only a month and everything is so confusing. I love it tho. thanks to all of you I've learned a lot!!!

My pleasure, Julian! Don't hesitate to ask questions if you ever have any! Happy coding! :)

Julian Martinez
Julian Martinez
3,094 Points

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 an item in our store. type 'list' to show all products ans '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 store'); } else { print (search + ' search is not in stock'); } } }