You've got a lot of duplication in your event handlers where each one only changes by the tip percentage amount. You can extract that into a reusable function that takes the tipPercent
as a parameter
You can then call that in your event handlers.
You could go a step further and add a data-tip-percent
value to each of your <button>
elements in your html. That way, instead of having separate event handlers for each button, you can have one event handler for all buttons and read the data-tip-percent
from the element using getAttribute
. You can then pass that to your function created in step one.
Going further still I would break that re-usable function into multiple smaller functions separating out functions that calculate a value from functions that change a display on the screen. That separates the concerns and would make it easier to update both the UI and the calculations if requirements changed in future.