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

92 lines
3.3 KiB
HTML

<html>
<head>
<title>Example 7.3</title>
<script>
function quotient(x,y)
{
illegal = "Illegal division operation";
if (y != 0)
return x/y;
else
return illegal;
}
function divideIt()
{
var divTop = parseFloat(prompt("Enter the divisor:"));
var divBottom = parseFloat(prompt("Enter the dividend:"));
document.getElementById('division').innerHTML = (divTop + " divided by " + divBottom);
var division = quotient(divTop, divBottom);
if (isNaN(division))
division = "illegal division operation";
else
division = division.toFixed(2);
document.getElementById('result').innerHTML = division;
}
function getMileage()
{
var miles = parseFloat(prompt("How many miles did your drive on this trip?"));
var gallons = parseFloat(prompt("How many gallons of gas did you use?"));
var trip = quotient(miles, gallons);
if (isNaN(trip))
{
trip = "illegal division operation";
document.getElementById('mileage').innerHTML = ("Cannot complete the calculation. " + trip);
}
else
{
trip = trip.toFixed(1);
document.getElementById('mileage').innerHTML = ("Your mileage for this trip was " + trip + " mpg.");
}
}
function getBMI()
{
var feet = parseFloat(prompt("How tall are you? Enter your height in feet first:"));
var inches = parseFloat(prompt("How many inches over " + feet + " feet are you?"));
var height = (feet * 12 + inches);
var hInches= height * height;
var weight = parseFloat(prompt("What is your weight in pounds? You may include a partial pound, like 128.5 lbs, for example."));
document.getElementById('height').innerHTML = (height.toFixed(2));
document.getElementById('weight').innerHTML = (weight.toFixed(2));
var bmi = (quotient(weight, hInches) * 703);
if (isNaN(bmi))
{
bmi = "illegal division operation";
document.getElementById('bmi').innerHTML = ("cannot complete the calculation. " + bmi);
}
else
{
bmi = bmi.toFixed(2);
document.getElementById('bmi').innerHTML = (" " + bmi);
}
}
</script>
<style type="text/css">
<!--
body {
margin: 5%;
}
-->
</style>
</head>
<body>
<h2>Using the quotient() function</h2>
<div style="width: 80%;">
<div style="width: 50%; float: left;"><fieldset><legend>Division Problem</legend>
<input type ="button" onclick="divideIt()" value = "Enter a division problem"></button>
<h2><span id = "division">&nbsp;</span></h2>
<h2>The result is: <span id = "result">&nbsp;</span></h2></fieldset></div>
<div style=" width: 50%; float: left;"><fieldset><legend>Gas Mileage</legend>
<input type ="button" onclick="getMileage()" value = "Find the gas mileage"></button>
<h2><span id = "mileage">&nbsp;</span></h2></fieldset></div>
<div style="clear:both;"></div></div>
<br />
<div style="width: 80%;"><fieldset><legend>BMI (Body Mass Index) Calculator</legend>
<p>The formula to calculate your BMI is your weight in pounds (lbs) divided by your height in inches (in) squared and multiplied by a conversion factor of 703. But don't worry about doing the math! If you enter your weight (lbs) and height (in feet and inches), the program will calculate your BMI.</p>
<input type ="button" onclick="getBMI()" value = "Calculate your BMI (Body Mass Index)"></button>
<h3>Your height (in inches): <span id = "height">&nbsp;</span></h3>
<h3>Your weight (in pounds): <span id = "weight">&nbsp;</span></h3>
<h3>Your BMI: <span id = "bmi">&nbsp;</span></h3>
</fieldset></div>
</body>
</html>