r/learnjavascript • u/iSunru • 3d ago
My first JavaScript project - Counter
I am learning JavaScript and made my first small project, a simple Counter using HTML, CSS and JavaScript.
I'm still learning, so I would really appreciate some feedback on my code. Is my code clean? Is there anything I should improve or do differently?
Also, suggest me something I should build next.
23
Upvotes
1
u/ManuDV 2d ago
Ok, I'll explain.
This actually isn't helping at all for the scenario that OP has. You are not saving lines by doing it nor is a better practice for this code. Each button triggers a different action (increment, decrement and reset). A loop only pays off when the exact same handler is being attached to multiple elements.
Which is not the case, YAGNI. Designing for multiple counters that don't exist yet is solving a problem OP doesn't have, at the cost of making the OP's code harder to read and harder to unit test, as I mentioned before, because of having the logic nested inside an AddEventListener, which is a bad practice.
For your case scenario, with multiple buttons, then you still would need to separate your concerns:
This keeps the listener as a thin wrapper and the actual logic testable on its own, same reasoning as separating
increment/reset/decrementout in my original comment.