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 trialMichael Rushton
7,126 Pointsclosing the li tags
Can't you close the li tags inside the first statusHTML parts as well as creating them?
for example
statusHTML += '<li class="out"></li>';
why do we do it after the conditional statements?
1 Answer
Steven Parker
231,269 PointsIf you did that, the list item would be empty instead of containing the employee name.
As shown in the video, the conditional code just creates the opening tag. The common code adds the employee name and then creates the closing tag. You wouldn't want to replicate that code in each branch of the condition when it can be done in just one place.
You could, however, replace the entire contents of the loop with an assignment using a template literal with two replacement tokens and a ternary expression for the class name:
statusHTML += `<li class="${employees[i].inoffice?'in':'out'}">${employees[i].name}</li>`;