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

andren
andren
28,383 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') {
    break;
  } 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.