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 app using HTML, CSS, and JavaScript

HaYeong 100

@hypyeon

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


Hi y'all..

I learned so much about JavaScript through this challenge. I learned how to code about a year ago but I wasn't consistent with my practice. While solving this challenge I realized how much I've forgotten and that I lacked practical skills.

For JS, I have these questions, if I may...

  • Overall, I believe there are functions that would make my coding more concise and less of so many repeated ones. If you have any advice, I welcome anything!
  • Can't seem to come up with how to '.toFixed(2)' bill amount. I want it where after you put amount and click elsewhere, it shows you rounded amount with 2 decimal places.
  • Couldn't figure out how to make results change according to the change of 'bill' and 'number of people', so I had to make it undo the 'tip selector' button as you change value of them so that after that you can 're-type' to see the results. Otherwise the previous result will remain with different button selected, if this makes sense.
  • How do I eliminated the arrow buttons that are automatically inside 'input[type=number]'? I tried '-moz-appearance: textfield;' and such, but it keeps getting reported as 'problems'.

I was really excited to solve this challenge, as I have only focused on HTML and CSS so far. I want to get better at JS quickly so any advice will help! :) Cheers!

Community feedback

Goran 500

@GoranK89

Posted

Hello HaYeong,

the calculator works, well done. Now for the JS part, I would recommend you try the forEach loop on all buttons except the custom one. What I mean is a setup something like this:

First the HTML setup for the buttons

            <input
              type="button"
              value="5%"
              class="btn-tip"
            />
            <input
              type="button"
              value="10%"
              class="btn-tip"
            />

Now we can gather all the buttons in JS and run a loop over them:

const tipButtons = document.querySelectorAll('.btn-tip');  // all the buttons

tipButtons.forEach(btn =>
  btn.addEventListener('click', function (e) {

    // on every clicked button remove the % symbol, only the value remains, store it in a variable
    const clicked = e.target.value.slice(0, -1); 

      // some sample calculations you can do with the "clicked" variable
      const billPerPerson = billAmmount.value / peopleNum.value;
      const tipPerPerson = (billPerPerson / 100) * clicked;
      const totalToPay = billPerPerson + tipPerPerson;
    
      // finally displaying the desired calculations example
      tipAmntPerPerson.textContent = '$' + tipPerPerson.toFixed(2);
    }
  })
);

You can also add some if/else statements inside the loop so for example calculations for "people < 1" are not done. This will probably shorten your code by 90% and clean it up a lot. Hope this was helpful and keep at it!

Marked as helpful

0

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