This site requires JavaScript, please enable it in your browser!
Greenfoot back
Extr3m3Unicorn
Extr3m3Unicorn wrote ...

2018/12/3

Need some one to look over my code and fix my errors for my tip calculator

Extr3m3Unicorn Extr3m3Unicorn

2018/12/3

#
function calculateTip() { var billAmt = document.getElementById("billamt").value; var serviceQual = document.getElementById("serviceQual").value; var numOfPeople = document.getElementById("peopleamt").value; //validate input if (billAmt === "" || serviceQual == 0) { alert("Please enter values"); return; } //Check to see if this input is empty or less than or equal to 1 if (numOfPeople === "" || numOfPeople <= 1) { numOfPeople = 1; document.getElementById("each").style.display = "none"; } else { document.getElementById("each").style.display = "block"; } //Calculate tip var total = billAmt * serviceQual / numOfPeople; //round to two decimal places total = Math.round(total * 100) / 100; //next line allows us to always have two digits after decimal point total = total.toFixed(2); //Display the tip document.getElementById("totalTip").style.display = "block"; document.getElementById("tip").innerHTML = total; } //Hide the tip amount on load document.getElementById("totalTip").style.display = "none"; document.getElementById("each").style.display = "none"; //click to call function document.getElementById("calculate").onclick = function() { calculateTip(); };
danpost danpost

2018/12/3

#
With a quick run-through, I do not see anything outlandishly wrong. Only thing I see that may be of concern is what range of values you allow for service quality and how the value is input. As currently coded, I would say it would be a decimal value between 0 and 0.25 (presuming 25% is maximum tip amount). Maybe its value should be verified to be in range before producing an answer.
Extr3m3Unicorn Extr3m3Unicorn

2018/12/4

#
could you show me where to change it because i am having trouble finding what you want me to change
danpost danpost

2018/12/5

#
Extr3m3Unicorn wrote...
could you show me where to change it because i am having trouble finding what you want me to change
I do not believe I said to change anything.
You need to login to post a reply.