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 trialSuneethaRani Savaram
Front End Web Development Techdegree Student 3,302 PointsArrays
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 the 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.indexOf(search) > -1) {
print ("yes,we have" + search + "item in the store.");
}
else {
print("sorry,we dont have "+ search + "in our store.");
}
}
}
why my code i not working?Can anybody help?
2 Answers
Cynthia Ajoy
4,857 PointsYes. Try adding single or double quotations when using the
if (search === "quit") and the if (search === "list")
it should work after that. Also when adding a string plus a variable to a message remember to add spaces
if(inStock.indexOf(search) > -1) { print("yes,we have" + search + "item in the store.");
if(inStock.indexOf(search) > -1) { print("yes, we have " + search + " item in the store."); }
Good luck :)
Chyno Deluxe
16,936 Pointsyou have your conditional statemenst searching for variables labeled quit and list rather then strings of "quit" and "list". Once you change those. It should work just fine.
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 the 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.indexOf(search) > -1) {
print ("yes,we have" + search + "item in the store.");
}
else {
print("sorry,we dont have "+ search + "in our store.");
}
}
}
I hope this helps.
Robert Komaromi
11,927 PointsRobert Komaromi
11,927 PointsThere are a few problems with your code. If you try to execute your code and examine the console output, or run your code through a JS linter (such as JSHint), it will quickly reveal some problems for you - give this a try.
Hint: You're missing a semicolon.
EDIT: After properly formatting your code, it seems the missing semicolon is not the problem (though you should still put one after your prompt assignment statement, if just to be consistent), and Chyno has beat me to the answer.