Solution
import "./styles.css";
const stars = document.querySelectorAll(".star");
let selectedValue = 0;
function handleStarClick(event) {
selectedValue = event.target.getAttribute("data-value");
stars.forEach((star) => {
star.classList.remove("active");
});
for (let i = 0; i < selectedValue; i++) {
stars[i].classList.add("active");
}
}
function handleStarHover(event) {
const hoveredValue = event.target.getAttribute("data-value");
for (let i = 0; i < hoveredValue; i++) {
stars[i].classList.add("active");
}
for (let i = hoveredValue; i < stars.length; i++) {
stars[i].classList.remove("active");
}
}
function handleMouseLeave() {
for (let i = selectedValue; i < stars.length; i++) {
stars[i].classList.remove("active");
}
for (let i = 0; i < selectedValue; i++) {
stars[i].classList.add("active");
}
}
stars.forEach((star) => {
star.addEventListener("click", handleStarClick);
star.addEventListener("mouseenter", handleStarHover);
star.addEventListener("mouseleave", handleMouseLeave);
});