The algorithm looks good to me. What I would change:
Use for
loops when iterating over a previously known range or sequence
for ( ; yellow >= 1; yellow--)
output += '<i class="fa fa-star text-yellow"></i>';
// ...
for ( ; white >= 1; white--)
output += '<i class="fa fa-star-o text-yellow"></i>';
for-loops are better at communicating what is being iterated over and there's a common recommendation to not alter the iteration counter outside of the loop header. Both of these individually and even more so together make it much easier to read and understand loops.
Of course some algorithms are more elegant and easier to understand if you disregard these recommendations. That's why they're recommendations for the majority of simple cases and not hard rules.
Instead of the loops above you can more elegantly (and probably more efficiently) use:
output += '<i class="fa fa-star text-yellow"></i>'.repeat(yellow);
// ...
output += '<i class="fa fa-star-o text-yellow"></i>'.repeat(white);
Manipulate DOM objects instead of generating HTML code in JavaScript
This may be a personal preference but I wouldn't even bother with HTML here.
var output = document.createElement("div");
output.title = rating;
for ( ; yellow >= 1; yellow--)
{
var star = document.createElement("i");
star.className = "fa fa-star text-yellow";
output.appendChild(star);
}
if (yellow == .5) {
var star = document.createElement("i");
star.className = "fa fa-star-half-o text-yellow";
output.appendChild(star);
}
for ( ; white >= 1; white--)
{
var star = document.createElement("i");
star.className = "fa fa-star-o text-yellow";
output.appendChild(star);
}
return output;
This will directly construct the DOM (sub-)tree that you're trying to describe in HTML.
Respect the intent behind HTML tags
<i>
is intended for text set in italic. However the use of “physical” formats in HTML is discouraged in favour of cascading styles (CSS). Use “logical” formats like <em>
or <strong>
or the other suggested tags on the previously linked MDN page to designate your intent and format them with CSS (or trust the client web browser to use a sensible default if you care about intent but not the actual resulting typesetting). If none of the pre-defined intents match your case, use <span>
and annotate it with classes, then format it with CSS.