JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Roudy Antenor
Roudy Antenor
4,124 Points

All OF My workers are playing hookie?!

Hello all, I have followed the video to the T - and i do not know why all of my workers appear to be in the red - yet the JSON file clearly has the inoffice properties of false and true for the workers - can anyone spot the error in my code?

let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (xhr.readyState === 4){
    let employees = JSON.parse(xhr.responseText);
    let statusHTML = '<ul class="bulleted">';
    for(let i=0 ; i < employees.length ; i++) {
      if(employees[1].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();

1 Answer

Seth Kroger
MOD
Seth Kroger
Treehouse Moderator 56,254 Points

In your if statement you are checking employees[1], so the employee's status won't be their status, but the status of the second employee in the list. You want to check it using the loop index, not a constant index.

Roudy Antenor
Roudy Antenor
4,124 Points

Awww man - Thank you for the review!! - it should've been employees[i] -- thanks for pointing out the error- cheers!