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

Robert Leonardi
Robert Leonardi
17,151 Points

Alternative solution to Meeting Rooms

Pls kindly suggest or comment. thanks

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (xhr.readyState === 4){
    if(xhr.status === 200 ){
      // typeof ==> to check type of data;
      let employees = JSON.parse(xhr.responseText);
      let add_ul = document.createElement("ul");
      add_ul.className = "bulleted";
      document.getElementById("employeeList").append(add_ul);
      employees.forEach(function(employee){
        let add_li = document.createElement("li");
        add_li.innerText = employee["name"];
        if(employee["inoffice"] == true){add_li.className="in"}else{add_li.className="out"};
        add_ul.append(add_li);
      });
    } else {
          alert("Error "+xhr.status+" due to "+xhr.statusText);
      }
  }
};
xhr.open('GET','data/employees.json');
xhr.send();


var xhr2 = new XMLHttpRequest();
xhr2.onreadystatechange = function () {
  if (xhr2.readyState === 4){
    if(xhr2.status === 200 ){
      // typeof ==> to check type of data;
      let rooms = JSON.parse(xhr2.responseText);
      let add_ul = document.createElement("ul");
      add_ul.className = "rooms";
      document.getElementById("roomList").append(add_ul);
      rooms.forEach(function(room){
        let add_li = document.createElement("li");
        add_li.innerText = room["room"];
        if(room["available"] == true){add_li.className="full"}else{add_li.className="empty"};
        add_ul.append(add_li);
      });
    } else {
          alert("Error "+xhr2.status+" due to "+xhr2.statusText);
      }
  }
};
xhr2.open('GET','data/rooms.json');
xhr2.send();

1 Answer