@NatureSon22
Posted
Your existing code has a repetitive structure for setting the rating when a button is clicked:
one.addEventListener("click",function(){
rating= 1;
select.innerHTML = "You selected " + rating+ " out of 5.";
submit.removeAttribute("disabled");
})
While this code is correct, it can lead to unnecessary repetition. As you progress in your coding journey, you will encounter iteration functions like for loops and forEach. One way to improve this code is to use the forEach method to iterate over each button element and extract its text content as the rating score:
let rating = 0;
button.forEach(btn => {
btn.addEventListener("click", function() {
rating = parseInt(btn.textContent);
select.innerHTML = "You selected " + rating + " out of 5.";
submit.removeAttribute("disabled");
});
});
Marked as helpful
@nina1234567896
Posted
@NatureSon22 Thankyou for your advice. I will definitely be using loopsin my other projects when needed =).