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 trialJana Ryndin
7,161 PointsLast action - searching for an item does not work
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 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 ' + seach + ' in the store');
} else {
print( search + ' is not in stock.');
}
}
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Jana,
You're missing the closing curly brace for your while loop. Maybe that was a copy/paste error?
The other issues is that you have a typo in the following code: (missing the 'r' in "search" in the print function call)
if (inStock.indexOf( search ) > -1) {
print( 'Yes, we have ' + seach + ' in the store');
It seems to be working fine when I tested it after making those 2 changes.
Let us know if it's still not working and any errors you might be getting in the console.
Jana Ryndin
7,161 PointsThank you so much that you took time to find that 'r'. I went back and forth several times and could not see it already. Now it works!
Ruven Koviar
3,917 PointsRuven Koviar
3,917 Pointsthis works
i think you had problem with .toLowerCase (see how i wrote it) and maybe you wanted to alert(which will send a massage from the browser) in the if and else , and not print.
edit this code as you want