import React from 'react'; const StarRating = ({ rating }) => { // Convert the rating to a number between 0 and 5 const normalizedRating = Math.min(Math.max(0, rating), 5); // Calculate the number of filled stars const filledStars = Math.floor(normalizedRating); // Calculate the number of half stars const hasHalfStar = normalizedRating - filledStars >= 0.5; return (
{[...Array(filledStars)].map((_, index) => ( ))} {hasHalfStar && } {[...Array(5 - filledStars - (hasHalfStar ? 1 : 0))].map((_, index) => ( ))}
); }; export default StarRating;