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 trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

Arrays

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?

Robert Komaromi
Robert Komaromi
11,927 Points

There 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.

2 Answers

Cynthia Ajoy
Cynthia Ajoy
4,857 Points

Yes. 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 :)

you 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.