59 lines
1.4 KiB
HTML
59 lines
1.4 KiB
HTML
|
<html>
|
||
|
<head>
|
||
|
<title>Example 7.8</title>
|
||
|
<script>
|
||
|
function getDiscount()
|
||
|
{
|
||
|
var item = " ";
|
||
|
item = prompt("What is the item you want to buy?");
|
||
|
cost = parseFloat(prompt("How much does this item cost?"));
|
||
|
cost = checkNum(cost);
|
||
|
if (cost < 50 && cost > 0)
|
||
|
{
|
||
|
rate = .10;
|
||
|
salePrice = cost * (1 - rate);
|
||
|
}
|
||
|
else
|
||
|
if (cost >= 50 && cost < 100)
|
||
|
{
|
||
|
rate = .15;
|
||
|
salePrice = cost * (1 - rate);
|
||
|
}
|
||
|
else
|
||
|
if (cost >= 100)
|
||
|
{
|
||
|
rate = .20;
|
||
|
salePrice = cost * (1 - rate);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
rate = 0;
|
||
|
salePrice = 0;
|
||
|
}
|
||
|
document.getElementById('item').innerHTML = item;
|
||
|
document.getElementById('orig_price').innerHTML = ("$ " + cost.toFixed(2));
|
||
|
document.getElementById('discount').innerHTML = ((rate * 100) + "%");
|
||
|
document.getElementById('result').innerHTML = ("$ " + salePrice.toFixed(2));
|
||
|
}
|
||
|
function checkNum(num)
|
||
|
{
|
||
|
if (num < 0)
|
||
|
{
|
||
|
alert("Invalid cost entered");
|
||
|
num = 0;
|
||
|
}
|
||
|
return num;
|
||
|
}
|
||
|
|
||
|
</script>
|
||
|
</head>
|
||
|
<body><table align="center" width = "70%"><tr><td><br />
|
||
|
<input type ="button" onclick="getDiscount()" value = "How much will you save? Find out now"></button><br />
|
||
|
<h3>You plan to purchase: <span id = "item"> </span></h3>
|
||
|
<h3>The original cost is: <span id = "orig_price"> </span></h3>
|
||
|
<h3>The discount rate is: <span id = "discount"> </span></h3>
|
||
|
<h3>You pay: <span id = "result"> </span></h3>
|
||
|
</td></tr></table>
|
||
|
</body>
|
||
|
</html>
|