Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

My code doesn't print anything until I type 'quit'

I followed the teacher's EXACT code but unlike his code, my code doesn't print anything until I type 'quit'. Does anyone know what's wrong? I am using Safari and I've tried the same code on Chrome as well, with both browsers giving the same issues.

Please, I need a fresh pair of eyes on it.

Here's my 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");
  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 + ' is not in stock.');
    }
  }
}
Trish Summers
Trish Summers
7,189 Points

Hi Kabir, I tried this code in Firefox and Chrome and it worked fine. You could try closing your browser and restarting it to make sure you can see the whole window.

Hi Kabir,

Did you get this resolved yet? I tried it out like Trish and it works for me too.

kabir k
kabir k
Courses Plus Student 18,036 Points

Hi Jason,

No, I haven't gotten it resolved yet.

I thought maybe there's something wrong with my code that's causing to give a different result from the teacher's.

3 Answers

Hi Kabir,

There's nothing wrong with your code, so rest easy there. The problem is Safari, but thankfully you won't be using document.write() much in actual programming. In fact, this sort of thing will call for something other than vanilla JavaScript. I've tried a few things, but Safari just doesn't want to update the document while that JavaScript loop is executing. I think your options at this point are to either use a different browser, use console.log() or make an alert instead of the print() function, or just wait until you quit the loop to see your program's output. The most important lesson here is that all browsers implement the same code differently, and sometimes that results in one being a particular PITA. In this case it's not a problem for the lesson, it's just kind of annoying.

kabir k
kabir k
Courses Plus Student 18,036 Points

True, quite annoying. Much appreciated, Greg.

Hello Kabir,

There is nothing wrong with your code.

 if ( search === 'quit' ) {
    break;

The first declaration of this if statement is what determines the end of this function. When you type the word "quit", it stops the function. Until this condition is met, the while loop will continue to run and print the other conditions. Let me know if you still have difficulty.

kabir k
kabir k
Courses Plus Student 18,036 Points

Hello Justin,

Thanks for taking a look at my code.

The problem is, unlike the teacher's code which print to the page even before he types the word 'quit', mine doesn't print to the page at all until I type 'quit' which is not quite the same result as the teacher's.

Trish Summers
Trish Summers
7,189 Points

Hi kabir, As you describe the problem you say the teacher's code prints to the page before he types 'quit'. Have you tried typing 'list' or a food item like 'carrot'? If you find that nothing prints out when you type 'list' you could try putting some debug code before each of the print statements. You could add an alert() before each print in your code and get a better idea of where the problem might be.

kabir k
kabir k
Courses Plus Student 18,036 Points

Yes, from the video, the result of his code prints to the page as he types for each search query. Yes, I have typed 'list' and an item from the array but it work like the teacher's. Ok, I will that. Thanks, Trish.

I'm not sure what to suggest for you code. Trish said there was no problem. I tried it out as well and it worked as expected.

After you type 'quit' do you end up getting all the expected output all at once? Or you never see any output from your program even after you type 'quit'?

When I tested I had typed 'list', an item that wasn't available, and then an item that was available. Each time I immediately saw the result.

Can you tell us how you're testing this? Is it in the console? Maybe we can reproduce your situation if we can follow your exact steps.

kabir k
kabir k
Courses Plus Student 18,036 Points

After typing 'quit', I ended up getting the expected output all at once instead piecemeal like the teacher's before finally typing 'quit' to exit the program. In my case, I don't see any of the results at all as I type the search terms BEFORE I type 'quit'. I only see them after I typed 'quit'.

Are you using the same code I posted?

No, I did not test it in the console. I previewed it on the page following the video.