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

Giovanni Dalla Rizza
Giovanni Dalla Rizza
10,637 Points

syntax error: what's wrong in my code?

I follow the lesson in my workspace and this is 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.lenght, 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();

But when I launch the preview it dowsn't work. Why? Can't find the error

Thanks

3 Answers

rydavim
rydavim
18,814 Points

You've actually got two small errors on that same line. There's the typo in length, but you also need to use ; instead of commas in your for expression.

for (var i=0; i<employees.length; i += 1) { // fixed length typo, fixed syntax error - change ',' to ';'
  // your code here 
}

That should do it, it's working for me now. Let me know if you're still having issues after those two though! Happy coding! :)

(employees.lenght) should be (employees.length)

Giovanni Dalla Rizza
Giovanni Dalla Rizza
10,637 Points

Right. I fix this now but it doesn't work yet

Piyush Patel
Piyush Patel
17,253 Points

Giovanni, on which line do you get the error? Is it with the send method?