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

Christine Bogdan
Christine Bogdan
5,339 Points

My code only prints to page after typing "quit". Why?

Hi guys,

I've written the code for the grocery search program. However, it only prints to the page, once I have "quit" the program, i.e. I have typed "quit". I can't see the difference between mine and Dave's code, apart from the fact that I have used the .includes() method, and not the .indexOf() method. But as far as I can tell, that should not be the problem.

Can someone tell me where I made a mistake? Thank you!

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.includes(search)) {
        print("You're lucky. We have " + search + " in stock!");
    } else {
      print("Sorry, we do not have " + search + " in stock!");  
    }
  }
 }

2 Answers

Steven Parker
Steven Parker
173,958 Points

You haven't made a mistake, that's normal browser behavior. If you look down at the Teacher's Notes below the video, you will see this notice:

Important Update

Since this video was shot, the behavior of most browsers has changed, 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.

Christine Bogdan
Christine Bogdan
5,339 Points

Thanks Steven Parker. That was a stupid question... I should have read the notes more closely!

Abdelmadjid Cherfaoui
Abdelmadjid Cherfaoui
3,229 Points

If you still want to see live search results, you can do a small edit on the code, I have made some improvement on the code so I can see live search results on the console instead of the webpage. Also, I let the user enter only one letter to quit or show up the list.

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

function print(message) {
  console.log( message );
}

while (true){
  search = prompt("Search for a product in our store. Type 'list' or 'L' to show all of the products and 'quit' or 'Q' to exit");
  search = search.toLowerCase();
  if (search === 'quit' || search ==='q'){
    break;
  } else if (search ==='list' || search ==='l'){
    print(inStock.join(', '));
    break;
  } else {
    if (inStock.indexOf(search) > -1){
        print('Yes, we have' + search + ' in the store.');
    }else {
        print(search +' is not in stock');
    }        
  }
}