Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. function calculateTip() {
  2. var billAmt = document.getElementById("billamt").value;
  3. var serviceQual = document.getElementById("serviceQual").value;
  4. var numOfPeople = document.getElementById("peopleamt").value;
  5.  
  6.  
  7. //validate input
  8. if (billAmt === "" || serviceQual == 0) {
  9. alert("Please enter values");
  10. return;
  11. }
  12. //Check to see if this input is empty or less than or equal to 1
  13. if (numOfPeople === "" || numOfPeople <= 1) {
  14. numOfPeople = 1;
  15. document.getElementById("each").style.display = "none";
  16. } else {
  17. document.getElementById("each").style.display = "block";
  18. }
  19.  
  20. //Calculate tip
  21. var total = (billAmt * serviceQual) / numOfPeople;
  22. //round to two decimal places
  23. total = Math.round(total * 100) / 100;
  24. //next line allows us to always have two digits after decimal point
  25. total = total.toFixed(2);
  26. //Display the tip
  27. document.getElementById("totalTip").style.display = "block";
  28. document.getElementById("tip").innerHTML = total;
  29. }
  30.  
  31.  
  32. //Hide the tip amount on load
  33. document.getElementById("totalTip").style.display = "none";
  34. document.getElementById("each").style.display = "none";
  35.  
  36. //click to call function
  37. document.getElementById("calculate").onclick = function() {
  38. calculateTip();
  39.  
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement