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

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,558 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.