Skip to content

Instantly share code, notes, and snippets.

@m4rrc0
Created June 9, 2022 10:25
Show Gist options
  • Save m4rrc0/05806de24f9aa5e1fb851beed64427d4 to your computer and use it in GitHub Desktop.
Save m4rrc0/05806de24f9aa5e1fb851beed64427d4 to your computer and use it in GitHub Desktop.
rating function ★★★☆☆
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment