ASDV-WebDev/Semester 1/Chapter ZIPs/JavaScript/ch7/ex_7_7.html

49 lines
1.2 KiB
HTML

<html>
<head>
<title>Example 7.7</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?"));
checkNum(cost);
if (cost < 50)
{
rate = .10;
salePrice = cost * (1 - rate);
}
else
if (cost < 100)
{
rate = .15;
salePrice = cost * (1 - rate);
}
else
{
rate = .20;
salePrice = cost * (1 - rate);
}
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");
}
</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">&nbsp;</span></h3>
<h3>The original cost is: <span id = "orig_price">&nbsp;</span></h3>
<h3>The discount rate is: <span id = "discount">&nbsp;</span></h3>
<h3>You pay: <span id = "result">&nbsp;</span></h3>
</td></tr></table>
</body>
</html>