Design comparison
SolutionDesign
Solution retrospective
How can I improve my JS code?
Community feedback
- @A-amonPosted almost 3 years ago
Hello! Great job~ š
Here are few suggestions:
- Use
button
orradiobutton
elements for the tip selection (Screen reader users won't know it's to be clicked if you use adiv
. - Change the
RESET
text toReset
. Find out more here š - Instead of using
innerHTML
for the texts, an alternative would betextContent
. You can read about their differences here. But for this current use case, both can be used. š - For the tip buttons' event listeners, there seems to be lots of repeated code. In this case, you can use a single function for them all. For example:
for(const tipBtn of tipButtons){ tipBtn.addEventListener('click', handleTipClick) } const handleTipClick = (event) => { let tipValue = 1 // Change it to whatever the default tip value is const selectedTip = event.target.value if(selectedTip === '5%'){ tipValue = 5 } let totBill = Number(inputBill.value)/100*tipValue let totPeople = Number(nrPersone.value) totTipOutput.innerHTML =(totBill/totPeople).toFixed(2) + "ā¬" }
The code above is written with the assumption that radio buttons are used š. Also, it might or might not work but, the general idea is there. š
Marked as helpful0 - Use
- @vtorre90Posted almost 3 years ago
Thank you for the suggestion, I was looking for a way to shorten my code š and you give me a great solution! Regarding the RESET comment is also interesting, I learn something absolutely new š
0
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