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 trialOscar Strömsäter
5,522 PointsEmployee List part doesnt load
I have tackled the Challenge in a slightly differnt approach by using forEach instead.
However when i reload my page the employee list part doesn't always work.
My code is:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4 && xhr.status === 200) {
var employees = JSON.parse(xhr.responseText);
const ulEmp = document.createElement('ul');
ulEmp.className = "bulleted"
document.getElementById('employeeList').appendChild(ulEmp);
employees.forEach((employee) => {
const liEmp = document.createElement('li');
const ulList = document.querySelector('ul');
ulList.appendChild(liEmp);
liEmp.className = employee.inoffice ? "in" : "out";
liEmp.textContent = employee.name;
});
}
};
xhr.open('GET', '../data/employees.json');
xhr.send();
const xhrRooms = new XMLHttpRequest();
xhrRooms.onreadystatechange = function () {
if(xhrRooms.readyState === 4 && xhrRooms.status === 200){
const rooms =JSON.parse(xhrRooms.responseText);
const ulRoom = document.createElement('ul')
ulRoom.className = "rooms"
document.getElementById('roomList').appendChild(ulRoom);
rooms.forEach((roomer) => {
const liRoom = document.createElement('li');
document.querySelector('#roomList ul').appendChild(liRoom);
if(roomer.available === false){
liRoom.className = "full";
}else {
liRoom.className ="empty";
}
liRoom.innerHTML = `${roomer.room}`
});
}};
xhrRooms.open('GET', '../data/rooms.json');
xhrRooms.send();
would appreciate any help on why this might be occuring