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

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
Seth Kroger
56,413 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!