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 trialMichael Elmallakh
2,531 PointsMy code runs only when I write quit
Please I want your help as the code runs only when I write quit
var instock = [ 'apple', 'Egg', 'milk', 'cheese', 'Carrot', 'Potato', 'Bread'];
var search;
function print(message){
document.write('<p>' + message + '</p>');
}
while (true) {
search = prompt("search for a product in a store. Type 'list' to show all the product or '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 stock');
} else{
print(search + 'is not in the stock');
}
}
}
8 Answers
Ayush Desai
1,386 PointsThis video was shot some time ago so now many browsers including your own only run all of your code when you type quit
Michal Janek
Front End Web Development Techdegree Graduate 30,654 PointsPlease see the Teacher´s notes. Browsers were updated.
David Bath
25,940 PointsWell, in your list "Egg", "Carrot", "Potato", and "Bread" are all capitalized, so your lower-cased search term will never match those items! I don't see why the other ones wouldn't work though.
Michael Elmallakh
2,531 PointsI don't know any way thanks Dav
Jonathan Shull
Courses Plus Student 12,739 PointsAssuming you are using chrome, I think it is just the way that chrome executes the code. If you try it out in Firefox it should print to the page while the loop is still running.
Michael Elmallakh
2,531 Pointsits working on firefox but if I write any thing it shows me that it appears on the stock :D
gary peart
6,496 PointsHi Michael,
I've looked at your code.
As pointed out by David above, you have used uppercase on the first letter of some of your array items, then lowercase on the first letter of other items. This will affect the search variable's value matching some of the items in your array.
I've tested your code and the issue regarding your last comment is caused by:
if ( instock.indexOf(search > -1)){
You almost have that line correct. Move the )
to the left and close the indexOf
method before the >
operator as below:
if ( instock.indexOf(search) > -1){
Hope this helps, if you've not resolved the issue yourself already ;o)
Dennis Amiel Domingo
17,813 PointsHi, Michael.
When you open the Workspace for this video, you'll notice that Dave posted a comment about this problem:
"/* Important note The behavior of most browsers has changed since this video was shot, 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. */"
Now, to solve this problem, replace all instances of "print" with "alert" and you'll see your list as well as the other messages.
Example:
From
print( instock.join(', '));
To
alert( instock.join(', '));
Anastasia Savina
6,165 PointsHi there! May be this way is not good, but if add break after each condition (if, else if and else) code is working well :)
Yan B
2,785 PointsI used break and it works.