Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Alex Thomas
Alex Thomas
4,247 Points

loops and arrays

https://w.trhou.se/uvabpfrt1q that is a link to the workspace for the lesson. I'm sure I typed it exactly as Dave did in the video lesson, however, it won't run for me. Can someone help out? Thx.

1 Answer

28,526 Points

There are two typos in your code:

  1. In your else if statement you have placed a semicolon ; after the conditions, which is incorrect. You don't use semicolons after if statements and the like. It causes them to be terminated before they are supposed to.

  2. You are missing a closing brace } at the end of your code.

If you fix those two issues like this:

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 of the produce and 'quit' to exit");
  search = search.toLowerCase();
  if ( search === 'quit') {
  } else if (search === 'list' ) { // <- Removed ;
    print( inStock.join(', ') );
  else {
    if ( inStock.indexOf( search ) > -1 ) {
      print('Yes, we have ' + search + ' in the store.');
    } else {
      print( search + ' is not in stock.');
}  // <- Added }

Then your code should work fine.

Alex Thomas
Alex Thomas
4,247 Points

haha I've been pulling my hair out all day trying to figure it out. I figured it was some little typo. BWAAAAHHHHHHHH. Thanks a lot man. I appreciate it.