Design comparison
Solution retrospective
'll like to know more on how I can make my JS code more precise and understandable to others.
Community feedback
- @DudeldupsPosted over 1 year ago
One very important thing to remember when writing any sort of code: If you repeat yourself in any way, there must be an easier way to write it. You can save all your buttons (give them a class, this way you can also style them easier in the CSS) in an array by using
const buttons = document.querySelectorAll(".yourButtonClassName") buttons.forEach(button => -- do something with each button -- )
and if you want to write a function to change a button, you can give that function a parameter (the button you want to change)
function changeButton(button) { button.addEventListener("click", () => { //do something on click } }
and then you could call the function in another forEach loop and your 150 lines of code are probably only 30 after that :)
Marked as helpful0
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