Skip to content

Instantly share code, notes, and snippets.

@benjaminEwhite
Last active December 30, 2015 22:29
Show Gist options
  • Save benjaminEwhite/7894207 to your computer and use it in GitHub Desktop.
Save benjaminEwhite/7894207 to your computer and use it in GitHub Desktop.
Bare Bones Tip Calculator Code for Thinkful's Programming Fundamentals in Javascript Module
<html>
<head>
<title>Tip Calculator</title>
<script src="script.js"></script>
</head>
<body>
<p>Hey there! Let me calculate that for you.</p>
</body>
</html>
function computeAmount(base, percentage) {
return base * percentage;
}
window.onload=function(){
var baseCost = parseFloat(prompt("Enter the dollar value for your meal, pre-tax (e.g., $15.74): $")); // We use parseFloat because the values we get from prompt will be cast to string type
var taxRate = parseFloat(prompt("Enter the tax rate as a percentage (e.g., 22): ")) / 100; // Divide by 100 to get to decimal value
var tipRate = parseFloat(prompt("Enter the percentage tip you'd like to leave (e.g., 15): ")) / 100;
var taxValue = computeAmount(baseCost, taxRate);
var preTipCost = taxValue + baseCost;
var tipValue = computeAmount(preTipCost, tipRate);
var totalCost = preTipCost + tipValue;
var message = ("Tax for your meal is $" + taxValue.toFixed(2) + '. ' +
"You should leave $" + tipValue.toFixed(2) + " for tip. " +
"The total cost for your meal is $" + totalCost.toFixed(2) + '.');
alert(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment