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 (retiring) Programming AJAX Processing JSON Data

Pieter Van Looy
Pieter Van Looy
7,764 Points

My code doesn't work

According to me, I have the same code as in the tutorial. Nevertheless, I get an syntax error message. What is wrong with my code?

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if(xhr.readyState === 4) {
    var employees = JSON.parse(xhr.responseText);
    var statusHTML = '<ul class="bulleted">';
    for (var i=0, i<employees.length; i += 1) {
      if (employees[i].inoffice === true) {
        statusHTML += '<li class="in">'; 
      } else {
        statusHTML += '<li class="out">'; 
      }
      statusHTML += employees[i].name;
      statusHTML += '</li>';
    }
    statusHTML += '<ul>';
    document.getElementById('employeeList').innerHTML = statusHTML;
  }
};
xhr.open('GET', 'data/employees.json');
xhr.send();

Edited for readabilty (Please review the Markdown Cheatsheet to see how to properly show code) - Dane E. Parchment Jr. (Moderator)

3 Answers

Shlomi Bittan
Shlomi Bittan
6,718 Points

Hi, Dane is right, the syntax error is from that comma. But i noticed another problem in your code that will cause it to fail. This line is wrong:

statusHTML += '<ul>';

You suppose to close the ul, so do this instead:

statusHTML += '</ul>';
Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

Well your syntax error is in your loop. Your first parameters are being separated by a comma instead of a semicolon. Is that the syntax error you are referring to?

Pieter Van Looy
Pieter Van Looy
7,764 Points

Hi guys or girls ;-),

that seems to be it. Thanks a lot!