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

Loop not working.

var userCity=prompt("what city are you from?")
var numElements = cleanestCities.length;
var matchFound = false;
var cleanestCities = ["Louisville", "New York", "Mexico City", "Japan"];
for (var i=0; i<numElements; i++) {
  if(userCity === cleanestCities[i]) {
    matchFound = true;
    alert("it's one of the cleanest cities")
    break
  }
}
if (matchFound) ===false) {
  alert("It's not on the list");
}

what am i doing wrong?

It looks like you have an extra parenthesis in this line: if (matchFound) ===false) {. Remove the one after the matchFound.

I just dont understand why the prompt does not pop.

3 Answers

Steven Parker
Steven Parker
229,920 Points

:point_right: There's a couple of issues preventing your code from running:

  • On line 2, you reference cleanestCities before it has been declared. Place this line after that is done.
  • On line 12, you have a stray closing parenthesis, as Jeremy indicated.

Optionally, you could condense line 12 to this:

if (!matchFound) {

place a ';' at the end of that line.

Steven Parker
Steven Parker
229,920 Points

:information_source: Semicolons at the end of a line are good practice, but they are completely optional.

Sean T. Unwin
Sean T. Unwin
28,690 Points

The variable, var numElements = cleanestCities.length; needs to be after cleanestCities has been declared. Unmatched closing parenthesis at if (matchFound) ===false) {.