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 Stage 2 Challenge Answer

Max Bardus
Max Bardus
11,794 Points

Any reason mine is not working? I broke my head!

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if(xhr.readyState === 4 && xhr.status === 200) {
    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();



var requestRoomList = new XMLHttpRequest();
requestRoomList.onreadystatechange = function() {
   if(requestRoomList.readyState === 4 && requestRoomList.status === 200) {
          var rooms = JSON.parse(requestRoomList.responseText);
          var statusHTML = '<ul class="rooms">';
          for (var i = 0; i < rooms.length, i++){
              if (rooms[1].available === true){
                  statusHTML += '<li class="empty">';
              } 
              else {
                  statusHTML += '<li class="full">';
              }
            statusHTML += rooms[i].room;
            statusHTML += '</li>';
          }
     statusHTML += '</ul>';
     document.getElementById('roomList').innerHTML = statusHTML;
   }
};

requestRoomList.open( 'GET', '../data/rooms.json');
requestRoomList.send();

Moderator edited: Markdown added so that code renders properly in the forums.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You have a teensy typo. You meant to type a semicolon but instead typed a comma. Take a look at line #29.

You wrote:

for (var i = 0; i < rooms.length, i++){

But you meant to write:

for (var i = 0; i < rooms.length; i++){
// note the semicolon between rooms.length and i++

:bulb: This is a good time to get used to checking the browser console for errors.

Hope this helps! :sparkles:

Along with the answer above, I also saw something wrong with the line below that.

if (rooms[1].available === true){

This should be 'i' instead of '1'. Otherwise this will only work on the first room.

As follows:

if (rooms[i].available === true){