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

I've been staring at this forever. Why isn't this working?

I was reviewing some older stuff on TreeHouse and I started watching videos I never finished. Since watching these particular JavaScript videos I have a acquired a bit more programming knowledge. Or at least so I thought. I was feeling confident I could recreate this Grocery List on my own. I opened up Codepen and started coding. I coded mine similar to Dave's but mine isn't working. I can't spot what I'm missing. Please help.

Here is Dave's code:

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 our entire store for products. Type "List" to see all products and "Quit" to exit.');
  if ( search.toUpperCase() === 'QUIT') {
    break;
  } else if ( search.toUpperCase() === 'LIST') {
      print( inStock.join(', '));
    } else if ( inStock.indexOf ( search ) > -1 ) {
      print('Yes, we have ' + search + ' in the store');
    } else {
      print('We do not have ' + search + ' in stock');
    }
}

Here is my code:

//Store groceries
var groceries = ["Eggs", "Butter", "Milk", "Bread", "Apples", "Grapes"];

var search;

function print(message) {
  document.write("<p>" + message + "</p>");
}

while (true) {
   search = prompt("What are you looking for? Type 'List' to see all and 'Quit' to exit");
  if (search.toUpperCase() === "QUIT") {
      break;
   } else if (search.toUpperCase() === "LIST") {
        print(groceries.join(", "));
   } else if (groceries.indexOf(search) > -1) {
        print("Yes we have " + search + " in stock");
   } else {
     print( "We do not have " + search + " in stock");
   }
}

3 Answers

Lee Ravenberg
Lee Ravenberg
10,018 Points

Hey Jeremy,

I tried to spot the difference between the two code examples. They are pretty much the same except that your grocery array is smaller and some variables are renamed.

Please tell us whats not working. Are you seeing the prompt? Or is it not showing up? You mentioned you are coding in Codepen. Are functions such as prompt() disabled there?

Tell us whats not working so we can help.

ps. when sharing code snippets, wrap them in Markdown markup so it will be joyful to read! :P

Thank you for the words of encouragement friend. Best of luck to you and happy coding :)

Mark Rinkel
Mark Rinkel
13,501 Points

I want to help... but this is really hard to read. I'll take a look if you checkout markdown syntax and reformat your code with proper indentation

Sorry about the format. I have never posted on here before. I have reformatted the code. To answer your question about not working.

And while I was reformatting my code I solved my problem. In the array I spelled all of the items with capital letters. I only used the toUpperCase() method on the "list" and "quit" commands. So in the prompt I was just searching eggs (all lower case) and it was saying the "not in stock". However when I searched Eggs (with a capital E) it worked. I forgot to convert the search using the toLowerCase() method. So I fixed my code to look like this:

var groceries = ["eggs", "butter", "milk", "bread", "apples", "grapes"];

var search;

function print(message) {
  document.write("<p>" + message + "</p>");
}

while (true) {
   search = prompt("What are you looking for? Type 'List' to see all and 'Quit' to exit");
   search = search.toLowerCase();
  if (search === "quit") {
      break;
   } else if (search === "list") {
        print(groceries.join(", "));
   } else if (groceries.indexOf(search) > -1) {
        print("Yes we have " + search + " in stock");
   } else {
     print( "We do not have " + search + " in stock");
   }
}

Honestly, making beginner mistakes like this is discouraging. I'm no where near a "seasoned" developer but I do feel like I know a decent amount. And to make such rookie mistakes is frustrating. I'm really thinking about taking a break from coding. I'm employed as a dev. I mainly work with HTML, CSS and Wordpress and occasionally JS so I play with code a lot. Recently Ive moved on to more advanced coding like working with API's and AJAX. I know this is off topic but how do you guys stay motivated when you make mistakes with such basic stuff like this? I don't know. It just seems like Ive forgotten a lot of the basic stuff since moving forward. Do you guys struggle with this stuff as well?

Mark Rinkel
Mark Rinkel
13,501 Points

Glad you solved the problem. I'm an entirely self taught programmer and it was a struggle for a long time. I certainly had my fair share of moments where I felt discouraged or that I would never reach a point of producing professional code, but the more you learn and the better you get at programming the more enjoyable it becomes. For me, I find it more enjoyable because I'm spending more and more of my time creating things instead of debugging simple problems.

Lee Ravenberg
Lee Ravenberg
10,018 Points

Hey Jeremy,

Glad you found the bug. I know it can be frustrating right! Can't remember how often I had to step away from the keyboard to prevent myself from having some sort of a mental breakdown lol.

I think you made the right decision to just post your question up here. I personally try to postpone asking for help as much as possible. Probably because I don't want others to think if me as a rookie or unskilled. The reality is, we are all in need of support once in a while. Great software and tech today exist not because of one or two genius minds that act solo, rather because of an open community where information is shared and where we teach our finding to others.

One of my goals in life is to create software that can have such an impact that it will improve peoples lives. That's why I started in tech in the first place. I try to remind myself this (like right now hehe) as often as possible to stay motivated. However it can be very discouraging when you are working on a project and you get stuck at things (trivail or non-trivial). Always remember that great achievement are only made by standing on the shoulders of giants. There is no shame in asking for help or taking advantage of others their work.

Here's an amazing talk: https://developer.apple.com/videos/play/wwdc2016/108/. You are very fortunate to be among the people that can code. Keep on doing what you do! ;)