Created
November 7, 2014 22:08
-
-
Save MadanThangavelu/2621df9cd1241087a630 to your computer and use it in GitHub Desktop.
Thinkful Tip Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Tip Calculator</title> | |
<script type="text/javascript"> | |
function createTaxCalculator(taxPercentage){ | |
return { | |
evaluateTax: function(preTipCost){ | |
return taxPercentage * preTipCost; | |
} | |
} | |
} | |
function createTipCalculator(tipPercentage){ | |
return { | |
evaluateTip: function(baseCost){ | |
return tipPercentage * baseCost; | |
} | |
} | |
} | |
function getUserPreferences(){ | |
var baseCost = parseFloat(prompt("Enter the dollar value for your meal, pre-tax (e.g., $15.74): $")); | |
var taxPercentage = parseFloat(prompt("Enter the tax rate as a percentage (e.g., 22): ")) / 100; | |
var tipPercentage = parseFloat(prompt("Enter the percentage tip you'd like to leave (e.g., 15): ")) / 100; | |
return { | |
'baseCost' : baseCost, | |
'taxPercentage' : taxPercentage, | |
'tipPercentage' : tipPercentage | |
} | |
} | |
window.onload=function(){ | |
var preferences = getUserPreferences(); | |
var tipCalculator = createTipCalculator(preferences.tipPercentage); | |
var taxCalculator = createTaxCalculator(preferences.taxPercentage); | |
var taxValue = taxCalculator.evaluateTax(preferences.baseCost); | |
var preTipCost = taxValue + preferences.baseCost; | |
var tipValue = tipCalculator.evaluateTip(preTipCost); | |
var totalCost = preTipCost + tipValue; | |
alert("Base Cost: " + preferences.baseCost + " Tax: " + taxValue.toFixed(2) + " Tip: " + tipValue.toFixed(2) + " TotalCost: " + totalCost.toFixed(2)); | |
} | |
</script> | |
</head> | |
<body> | |
<p>Hey there! Let me calculate that for you.</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment