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

What is wrong with this code?

Hi, I can't seem to find out what is wrong with my Javascript code, some help would be appreaciated!

// Step 1 - Create request object.
var request = new XMLHttpRequest();

// Step 2 - Create callback function, will be invoked when a request has been received.
request.onreadystatechange = function () {

  if (request.readyState === 4) {

    rooms = JSON.parse(request.responseText);
    setHTML = '<ul class="rooms">'

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

      if(rooms[i].available == true ) {
        setHTML += '<li class="empty">';
      }
      else {
        setHTML += '<li class="full">';
      }
      setHTML += rooms[i].room;
      setHTML += '</li>';
    }

    setHTML = '</ul>';
    document.getElementById('roomList').innerHTML = setHTML;
  }

}

//Step 3 - Making the request object.
request.open('GET', '../data/rooms.json');

//Step 4 - Sending the object
request.send();

1 Answer

Similar to what happened the last time. On line 24 you have:

   setHTML = '</ul>';

it should be

   setHTML += '</ul>';

AAARGHHHHHHH, thank you again!