In: Computer Science
Javascript Calculator
Algorithm to calculate a tip percentage given the bill amount and total bill including tip.
Asker suer for bill without tip:
Ask the user for total bill with tip:
Ask the user how many people splitting bill:
Submit button to calculate the tip percentage
HTML & Javascript:
<!DOCTYPE html>
<html>
<head>
<script
type="text/javascript">
//Function that
calculates tip percentage
function
calculateTipPercentage() {
//Reading input from user
var billWithoutTip =
parseFloat(document.getElementById("W_O_Tip").value);
var billWithTip =
parseFloat(document.getElementById("W_Tip").value);
var numPeople =
parseFloat(document.getElementById("numP").value);
//Calculating tip amount
var tipAmt = billWithTip - billWithoutTip;
//Calculating tip percentage
var tipPercentage = (billWithTip -
billWithoutTip) / 100;
//Writing result
document.getElementById("Tip_Per").value =
tipPercentage + "%";
}
</script>
</head>
<body>
Enter total bill without tip:
<input type="text" id="W_O_Tip"> </br></br>
Enter total bill with tip:
<input type="text" id="W_Tip"> </br></br>
Enter number of people splitting
the bill: <input type="text" id="numP">
</br></br>
<input type="submit"
value="Submit" onclick="calculateTipPercentage()">
</br></br>
Tip Percentage: <input
type="text" id="Tip_Per" readonly=true>
</br></br>
</body>
</html>
__________________________________________________________________________________________
Sample Run: