Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted 10 months ago

Tip Calculator App

Dhia•710
@Dhia-18
A solution to the Tip calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

I'm still learning JavaScript, but I feel like I'm making good progress. In this recent challenge, I'm particularly proud of the clean and organized code I created.

What challenges did you encounter, and how did you overcome them?

The biggest challenge I faced was figuring out how to structure my JavaScript code properly while accounting for all the edge cases in the app

What specific areas of your project would you like help with?

I'd appreciate it if someone could review my code and offer suggestions on how to improve it.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Gregor de Cillia•190
    @GregorDeCillia
    Posted 3 days ago

    Nice work!

    You said you are mainly interested in feedback on the JS, so I'll focus on that. Before I get to some recommendations, I want to state that your code is very organized and the naming decisions were great. This is a very important aspect to keep growing whether you do markup languages or coding languages.

    Consider Continuous Evaluation

    It initially took me some time to ralise the outputs are only generated from the return key. It might be better to update them continuously as the user types. If things start lagging as a result (unlikely), you could add a debounce.

    Helper functions for input checks

    The input checks for the three inputs are very similar. To avoid duplication, you could add a helper function that performs the checks.

    function inputValid(input, error, checkZero = false) {
        if(input.value===""){
            showError(billAmount,error,"Can't be empty");
            return false;
        }
        else if(checkZero && input.value==="0"){
            showError(billAmount,error,"Can't be zero");
            return false;
        }
        else if(!isFloat(input.value)){
            showError(input,error,"Must be a number");
            return false;
        }
        return true;
    }
    

    Use the built-in validity checks

    Inputs have a validity property which could be used to simplify some of the logic.

    billAmount.validity
    // {badInput: false, rangeUnderflow: false, valueMissing: false,
    //   valid: true, ...}
    

    For example, the isFloat() helper function could be replaced by a check against badInput. If you set min/max/step on an input, you can also see if things are inside or outside the range and wether the number of decimals is too high.

    <input type="text" id="number-of-people" placeholder="0"
     min="1" max="8005176000" step="1">
    
    if (numberOfPeople.validity.badInput) 
      showError(numberOfPeople,numberOfPeopleError,"Must be a number")
    if (numberOfPeople.validity.rangeOverflow)
      showError(numberOfPeople,numberOfPeopleError,"more people than world population")
    if (numberOfPeople.validity.stepMismatch)
      showError(numberOfPeople,numberOfPeopleError,"must be an integer")
    

    Closing remarks

    Very nice how you used <form> and <input type="radio">. It was also very interesting to see how you used grid for some things, where I used 2 levels of flex instead (e.g. .bill-input).

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
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit 1st-party linked stylesheets, and styles within <style> tags.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub