JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Giovanni Dalla Rizza
Giovanni Dalla Rizza
10,633 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

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

Giovanni Dalla Rizza
Giovanni Dalla Rizza
10,633 Points

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

rydavim
MOD
rydavim
Treehouse Moderator 17,951 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! :)

Piyush Patel
Piyush Patel
17,253 Points

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