Tip Calculator App

Solution retrospective
I am alway proud when I complete a challenge, not giving up is a big win for me. Other than that gaining a better understanding of how to work with numbers for calculations in JavaScript.
What challenges did you encounter, and how did you overcome them?- Converting the input values to numbers to calculate was tricky. I was originally using
parseInt()
but it was giving me issues at times, so I learned for my case it was best to useNumber()
- Also dealing with the arrows in the number input and their spacing, I learned there was way to remove them.
Definitely the JavaScript. I would like feedback on what I have and if I could have made it more efficient.
Please log in to post a comment
Log in with GitHubCommunity feedback
- P@ValeriaMontoya
Hi @EmLopezDev 👋🏻
Your JavaScript code is generally well-structured and understandable. However, there are a few areas where improvements can be made. Here’s a breakdown:
Typo
const addAcitveTip = (evt) => { ... }
addAcitveTip
should beaddActiveTip
.Repeated DOM manipulation
You're callingtipButtons.forEach()
multiple times in bothremoveActiveTip()
andaddAcitveTip()
. These can be merged for better performance and clarity:const setActiveTip = (evt) => { tipButtons.forEach((button) => { const isActive = button.id === evt.target.id; button.classList.toggle("active", isActive); if (isActive) tipCustom.value = ""; }); }; const addTip = (evt, value) => { setActiveTip(evt); tip = value ? Number(value) : null; showResults(); };
Inefficient
showResults()
logic
You're checking the same condition twice with a lot of repeated code. Combine conditions using else and simplify:const showResults = () => { const errors = { bill: !bill, tip: tip === null, people: !people, }; billErrorSpan.innerHTML = errors.bill ? "Can't be zero" : ""; tipErrorSpan.innerHTML = errors.tip ? "Must select tip" : ""; peopleErrorSpan.innerHTML = errors.people ? "Can't be zero" : ""; if (errors.bill || errors.tip || errors.people) { tipPerPersonSpan.innerHTML = "$0.00"; totalPerPersonSpan.innerHTML = "$0.00"; resetButton.setAttribute("disabled", true); } else { const perPerson = formattedResults(); tipPerPersonSpan.innerHTML = perPerson.tip; totalPerPersonSpan.innerHTML = perPerson.total; resetButton.removeAttribute("disabled"); } };
I really hope my feedback helped, keep coding! 👩🏻💻
Marked as helpful
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