Design comparison
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
- @elaineleungPosted over 2 years ago
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:
-
Generally it is good practice to use
const
overlet
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 useconst
for variables. The only variable I would uselet
for here is thenumberSelected
one because that's bound to change. -
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 helpful1@albana-meloniPosted over 2 years ago@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@elaineleungPosted over 2 years ago@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 GitHubJoin 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