Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Interactive rating component with JS

Albana 40

@albana-meloni

Desktop design screenshot for the Interactive rating component coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
1newbie
View challenge

Design comparison


SolutionDesign

Solution retrospective


HI! It's there any other way to write the JS code? I think it works well, but I thought maybe not using buttons for the number rating and something like a select element and wrap them all on a form... I don't know. Tell me how you did/would do it!! Thanks <3

Community feedback

Elaine 11,400

@elaineleung

Posted

Hi Albana, well done on your second challenge, and I think what you did using buttons is a good start! 😊 That was also what I did when I first completed the challenge, and it was only later when I updated it that I rewrote it using radio buttons.

About the JavaScript and HTML in general, I think you did a lot of things well here. Your method of nesting the submit button event listener within the button event listener is an interesting solution in preventing the user from proceeding without a rating, and it's probably the first one I've seen that does that. Anyway, the two suggestions I would offer are as follows:

  1. Generally it is good practice to use const over let for variables that don't change (such as elements from the DOM). This is just to prevent an accident reassignment from happening, and I'd say in 90% of the situations you'd want to use const for variables. The only variable I would use let for here is the numberSelected one because that's bound to change.

  2. Instead of using the innerHTML for retrieving the value, I'd suggest writing the value as an attribute within the button (e.g., <button class="rate-number" value="5">5</button>). I just think it's a safer way to retrieve information from the element's attributes than from its innerHTML, which could change in certain scenarios, like if the innerHTML got written over somehow.

If you're interested in checking out other ways to do this challenge, I have a small CodePen collection with various solutions, including the radio button one: https://codepen.io/collection/oErbpe

Great work here, and looking forward to more 😊

Marked as helpful

1

Albana 40

@albana-meloni

Posted

@elaineleung Hi!! I'm very grateful for your message! It inspires me to keep learning and practicing. I really appreciate your recommendations to improve the code, I'll keep them in mind for the next challenge. Thank you!

1
Elaine 11,400

@elaineleung

Posted

@albana-meloni You're very welcome, and keep going, you're already doing a great job! 😊

1

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord