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

11 {'s. Am I counting this correctly?

Following along with this lesson, my code isn't working. According to jslint, I have a missing {, but I am not sure where it is supposed to be.

Did the teacher get it wrong? Did I get it wrong? Id he got it wrong, how did his code still work?

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') {
     print(inStock.join(", ") );

  }  else {
    if (inStock.indexOf( search ) > -1) {
    print("Yes, we have " + search + " in the store.");
  } else  {
    print( search + " is not available.");
      }
}

3 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Edit: I reformatted the code for better readability as Josh Olson mentioned. However, you should note that two space indent is still acceptable just be consistent with it. If you intend to use JSLint a lot stick with 4 space indent.

The problem is unclosed while statement. Fix your indentation and you will start to see the mistake.

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') {
        print(inStock.join(", "));
    } else {
        if (inStock.indexOf(search) > -1) {
            print("Yes, we have " + search + " in the store.");
        } else {
            print(search + " is not available.");
        }
    } // You missed this extra bracket. which closes top level else statement.
}
Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Well comment on OP not me. All it does to this code is to have 4 space indent level.

Quantice Appleton
Quantice Appleton
2,775 Points

Thanks for this input. I was having the same problem when I discovered that I had a missing closing bracket. I followed along with the teacher exactly as well so I am not sure how I got this wrong but the coding would not work until I added the missing closing bracket.

I think it's the second 'else if'. You write:

else { if (....)

It should be

else if (...) {...}

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Just because the code looks like else { if () {}} it doesn't mean it's typo of else if () {}. They are different logic.

else if and else { if have two different meanings. With else if, you only execute the following code if the condition is met. In the else { if code, the else statement always runs. In the code above, there is an error using the else { if because the final else will never run. Once you hit the first else, the conditional ends.

There is no elseif in JavaScript.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Sorry but else if does exist in JavaScript and evidence is plenty.. For example, your link. He just wanted another control flow for else statement.

You are correct that else space if does exist as we both stated. If you look at my link to MDN on JavaScript you will see that they clearly say elseif (no space) does not exist in JavaScript. The two have distinctly different meanings in languages where elseif exists like PHP.