Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Tip calculator made using CSS Flexbox/Grid and Javascript

@Meobot

Desktop design screenshot for the Tip calculator app coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


I realize now that there are some areas in JS that could use work, such as keeping the current tip % highlighted, and possibly condensing all of the event listeners.

I feel like I wrote way too much code for this application, so any tips on refactoring / best practices would be greatly appreciated.

Community feedback

Elaine 11,400

@elaineleung

Posted

Hi Meobot, your solution looks great! Regarding functionality, it looks like if I click on the button before inputting the number of people, the total area shows "infinity". What I did was, I added a line in my code that checks whether all the 3 values are there before calculating, and that way it won't show other unwanted results. Also, it would be good to have the button "stick" as a pressed state after clicking because right now it just goes back to looking like other buttons after being pressed.

Regarding refactoring, I see that you got an event listener written for each individual button. I would just use one. What I'd do is, I'd add a value directly onto each button, like this:

<button class="btn" id="five" value="5">5%</button>

And then I'd grab all the buttons and put an event listener on each one, something like this:

document.querySelectorAll(".btn").forEach( btn => {
  btn.addEventListener("click", () => {
    tip = btn.value;
    calculateFinal(tip);
  })
})

I think this can save you a few lines of code! Hope this is helpful 🙂

Marked as helpful

1

@Meobot

Posted

@elaineleung I had no idea you could put a value on a button like that, that's awesome! Thank you for the advice, I'll definitely implement all of this in the project, and others going forward.

1

Please log in to post a comment

Log in with GitHub
Discord logo

Join 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