All Challenges

Interactive Star Rating Widget

HTML/CSSEasySwiggyZomatoBookMyShow
30:00

Problem Brief

Build an accessible 5-star rating control supporting hover previews, click selection, keyboard navigation, and live rating text.

Requirements

  • Render 5 interactive star buttons inside `[data-testid="star-group"]` with `aria-label` values.
  • Hovering star N previews stars 1..N with an `.is-active` class without changing committed rating.
  • Clicking star N commits the rating, sets `aria-pressed="true"` on stars 1..N, and updates `[data-testid="rating-label"]`.
  • Clicking Reset (`[data-testid="reset-rating"]`) clears selection back to `0 / 5`.
Progressive Hints (2)
  • Track two variables: `selectedRating` (0-5) and `hoverRating` (0-5). Active stars = `starIndex <= (hoverRating || selectedRating)`.
  • Use event delegation or attach `mouseenter`, `mouseleave`, and `click` on `[data-star]` buttons.
Reference Solution

Stuck? Load the verified reference code into your editor.

Automated Test Suite

5 Test Cases Ready
  • Renders 5 accessible star buttons5 buttons exist inside [data-testid="star-group"] with aria-label
  • Clicking 4th star sets 4 active stars and labelMarks stars 1..4 active + aria-pressed="true" and updates rating-label
  • Hover previews 5 stars without mutating committed ratingMouseenter highlights preview stars and mouseleave restores selected rating
  • Reset clears rating back to 0 / 5Resets active classes and label text on reset button click
  • Updating selection downwards clears higher starsClicking 2nd star after 5th star leaves only 2 active stars
solution.html
Live Sandboxed Previewiframe sandbox
Starter template loaded — add your <script> logic in the editor (or open Reference Solution) to make the widget interactive.