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 A Real World Example

Simpler way to write generateListItems using template literal

I updated generateListItems to use template literals and a for-of loop in case anyone is interested here is the code.

function generatListItems(employees)  {
    let statusHTML = '';
    for (const employee of employees) {
        statusHTML += `<li class="${(employee.inoffice === true) ? "in" : "out"}">${employee.name}</li>`
    }
    return statusHTML;
}

1 Answer

Steven Parker
Steven Parker
229,982 Points

Well, it's certainly more compact, but I'm not sure I'd say "simpler". It could be a bit more of a challenge for someone reading it to quickly grasp the functionality.

However, that said, I'm also a fan of compact code. I might write it this way:

const generateListItems = employees => employees.reduce(
    (s, e) => s + `<li class="${e.inoffice ? "in" : "out"}">${e.name}</li>`, ""
);