JavaScript AJAX Basics (retiring) Programming AJAX Stage 2 Challenge Answer

Eamon Jurdzis
Eamon Jurdzis
8,849 Points

My version of the loops

Just thought I'd share my version of the code that generates the list items. Quite proud of it!

var roomsHTML = '<ul class="rooms">';

for (var i = 0; i < rooms.length; i += 1) {
  roomsHTML += '<li class="' + (rooms[i].available === true ? 'empty' : 'full') + '">' + rooms[i].room + '</li>';
}

roomsHTML += '</ul>';

1 Answer

Steven Parker
Steven Parker
172,127 Points

Nice use of the ternary. :+1:   But you never need to compare a boolean to "true", so you can simplify it to this:

    rooms[i].available ? 'empty' : 'full'
Eamon Jurdzis
Eamon Jurdzis
8,849 Points

Thanks for the great feedback! I suppose my thinking at the time was that the content of rooms[i].available may not necessarily be boolean. In this case it is, so yeah I should have left that out!