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 trialChristopher Jauregui
1,967 PointsWhy won't my grocery list display when i type "list" i have to type exit for prompt to quit then view.
06:54 into the video he runs the code and the list will display behind prompt then he types quit to exit prompt. When i run the same code I it will not show until i type quit the grocery list shows , but if i add a "break" at the end of the else if statement it works? 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 the product in the store. Type 'list' to show al of the products in the stock and 'break' to exit" ); if (search === 'quit'){ break;
}else if (search === 'list'){ print(inStock.join(', ')); break; } }
4 Answers
andren
28,558 PointsThis is explained in the teacher's notes of the video:
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.
Sorry for the confusion, and we'll update the video soon.
Andrea Dinh
4,946 PointsHi, this might be a late contribution, but I had the same problem, and then I figured I could just put "break" after "print" like this: else if ( request === "list" ) { print( inStock.join(", ") ); break; }
Hope this helps
Jeffrey Libatique
6,701 PointsHi Andrea,
I did your suggestion and it worked.
Thanx a lot!
Saqib Ishfaq
13,912 PointsThanks andrea, just came across ur answer here, yes it helped by adding " break; " after "print". do u mean just on one occasion? coz as per teacher's notes, for browsers its not been working according to the code, adding "break; " after every "print" works just fine.
Jarom Denny
4,081 PointsIf you write break after each print function you can only call one test at a time. The other option is to add an alert to the print function so you get real time responses. i.e. function print(message) { document.write( '<p>' + message + '</p>'); alert(message); }
alee2
5,435 PointsThanks Andrea Dinh this works!
hope the video get updated soon.
Christopher Jauregui
1,967 PointsChristopher Jauregui
1,967 PointsWow first time i have gotten a response from the actual person in the video. This is the first question i have had for you guys good to know you guys have a responsive team . Thanks for the help.
danielperez
Full Stack JavaScript Techdegree Student 15,993 Pointsdanielperez
Full Stack JavaScript Techdegree Student 15,993 Points