Skip to content

Instantly share code, notes, and snippets.

@m4rrc0
Created June 9, 2022 10:25
Show Gist options
  • Select an option

  • Save m4rrc0/05806de24f9aa5e1fb851beed64427d4 to your computer and use it in GitHub Desktop.

Select an option

Save m4rrc0/05806de24f9aa5e1fb851beed64427d4 to your computer and use it in GitHub Desktop.

Revisions

  1. m4rrc0 created this gist Jun 9, 2022.
    16 changes: 16 additions & 0 deletions rating.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    // Credits: https://twitter.com/builderio/status/1534246051892408320

    const rating = stars => '★★★★★☆☆☆☆☆'.slice(5 - stars, 10 - stars);

    // OR

    const rating = max => star => "★".repeat(Math.min(star, max)).padEnd(max,"☆");

    // It caps the value to max then gets an input number of stars
    // Examples:
    const rating5 = rating(5);
    rating5(3); // "★★★☆☆"

    const rating3 = rating(3);
    const rating10 = rating(10);
    const like = rating(1);