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 trialDennis Klarenbeek
17,168 PointsCan somebody help me why this code isn't working?
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('He just fill in a product you want and we look in our stock. If you want to see all items please type in "list". If you want to close this windows, please type "quit".');
search = search.toLowerCase();
if ( search === 'quit') {
break;
} else if (search === 'list') {
print(inStock.join(', '));
} else {
if (instock.indexOf(search) > -1) {
print('We got ' + search ' on stock');
} else {
print('Sorry, ' + search ' is not on stock');
}
}
}
3 Answers
Stephen Gheysens
11,935 PointsIt was a great explanation! (I'm also always impressed with people's understanding of English when it is not their primary language, so good job on that, too)
I found at least one error: in the large else condition, you have an "if" and "else" that each print out a string. You concatenate the first two strings ("We got" and search/"Sorry" and search), but you're missing a second "+" in each of those terms to concatenate to the last part of the output string (i.e. " is in stock").
Dennis Klarenbeek
17,168 PointsThank you for the reply and for the compliment. It's now my turn to look in this code again because when I fill in anything there's nothing printed on the screen. But the list works.
Dennis Klarenbeek
17,168 PointsAlready found the problem the caller was not right (instock instead of inStock).
Thank you again!
Dennis Klarenbeek
17,168 PointsHe should give me a prompt screen where I can fill in a product I look for (products in the array) when I fill in quit the prompt screen will go away, when i fill in list there we'll a list of all items in the array on the screen. When I fill in the product that is in the Array there we'll be printed a message [We got ' + search ' on stock'] and when the product is not in the array there we'll be printed a message [Sorry, ' + search ' is not on stock].
Right now when I run this nothing running. When I delete the last Else statement it works fine, without the opportunity to look for the products in the array of course.
I hope I define the problem good enough
Stephen Gheysens
11,935 PointsStephen Gheysens
11,935 PointsHi Dennis. When you say the code "isn't working", could you describe the behavior? What are you expecting to happen, and what is currently happening?