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 trialChristine Bogdan
11,349 PointsMy 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!");
}
}
}
3 Answers
Steven Parker
231,268 PointsYou 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
11,349 PointsThanks Steven Parker. That was a stupid question... I should have read the notes more closely!
Abdelmadjid Cherfaoui
3,230 PointsIf 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');
}
}
}
Marc Sanchez
20,039 PointsFor future reference, to get the same behavior as in the video, you can just add a break after the print method in the else if statement. If the user enters 'list' and clicks 'Ok' it'll print the list to the page as it does in the video.
Kevin Redmon
6,326 PointsThat works for me, thanks.
Marc Sanchez
20,039 PointsMarc Sanchez
20,039 PointsFor future reference, to get the same behavior as in the video, you can just add a break after the print method in the else if statement. If the user enters 'list' and clicks 'Ok' it'll print the list to the page as it does in the video.