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 trialColton Shaw
12,634 PointsAlternate Solution for Processing JSON
I'm sure there is a better way to do this, but I solved this before the video finished and came up with a solution that I felt looked a little cleaner.
I did modify the css file to be the below to make this a bit easier to parse.
ul.bulleted .true {
color: #5A6772;
}
ul.bulleted .true:before {
content: "IN";
background-color: #5fcf80;
}
ul.bulleted .false {
color: #A7B4BF;
}
ul.bulleted .false:before {
content: "OUT";
background-color: #ed5a5a;
}
Actual Code:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4) {
let response = JSON.parse(xhr.responseText);
//created an empty employees string
let htmlEmployees = "";
for (i = 0 ; i < response.length ; i++){
let employee = response[i]
//adding to that string each time with a simple LI
htmlEmployees += `<li class="${employee.inoffice}">${employee.name}</li>`
};
//created a employe ul element that places my employee string in the middle and outputs it to the html.
let html =
`<ul class="bulleted">${htmlEmployees}</ul>`
document.getElementById('employeeList').innerHTML = html;
}
};
xhr.open('GET','data/employees.json');
xhr.send();
1 Answer
sean shea
22,104 PointsTreehouse "recently" updated a big chunk of the Full Stack JavaScript track, and this section was not part of that update. It's unfortunate because this section feels out of date compared to the other. If anything, I guess it shows they are working on updating old content.
Nice job on the solution btw. Below is the one I came up with.
let xhr = new XMLHttpRequest();
let html = "";
let inoffice = "";
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let employees = JSON.parse(xhr.responseText);
for (i = 0; i < employees.length; i++) {
if (employees[i].inoffice) {
inoffice = "in";
} else {
inoffice = "out";
}
html += `<li class="${inoffice}"> ${employees[i].name} </li>`;
}
document.getElementById('employeeList').innerHTML = `<ul class="bulleted" > ${html} </ul>`;
}
}
}
xhr.open('GET', "data/employees.json");
xhr.send();