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 AJAX Basics Programming AJAX Processing JSON Data

Else statement error, but else syntax seems fine.

The snapshot is at http://w.trhou.se/89or2kaat3.

There is an error (syntax error: unexpected token 'else'). The brackets are there and the if-else syntax seems good. Not sure why it's having this issue.

2 Answers

Gergely Bocz
Gergely Bocz
14,244 Points

The syntax problem comes from having a for inbetween the if and else statements. It happens because you missed out on the if that is inside the for. You are missing this piece of code inside for:

if(employees[i].inoffice === true){
 statusHTML += '<li class="in">';
}

So try replacing your for statement with this piece of code:

for (var i=0; i<employees.length; i += 1){
if(employees[i].inoffice === true){
 statusHTML += '<li class="in">';
 console.log(statusHTML);
}else {
   statusHTML += '<li class="out">';
  }
 statusHTML += employees[i].name;
 statusHTML += '</li>';
}
Gergely Bocz
Gergely Bocz
14,244 Points

Hi Jonathan!

Your else statement is inside the if statement.

This will probably sound really stupid, but is it not supposed to be? I updated the file to close the if statement prior to the for loop, but still get the same error for the else statement.

Here's the new snapshot: HTTP://w.trhou.se/y0ch5sqs3a