Make your own free website on Tripod.com
<!DOCTYPE html public "-//w3c//dtd html 4.0 Transitional//en">

<HTML>

<HEAD>
<TITLE>Gas Mileage Calculator</TITLE>
<META name="GENERATOR" content="Arachnophilia 4.0">
<META name="FORMATTER" content="Arachnophilia 4.0">
<P>
<center><h2>Exercise 9.11</h2>
<table width=375>
  <tr>
    <td align='center'>
      The Gas Mileage Calculator will calculate the miles per gallon for up to 10 tanks of gas,
 and the average miles per gallon for all tankfulls.<br>
 (Enter -1 gallons when you have entered all your tankfulls.)
    </td>
  </tr>
</table></center>
<script language="JavaScript">
<!-- ;
var gals, miles, per, tanks, avg = 0;

// Output headers
document.writeln( "<P><TABLE align='center' border='1' cellspacing='0' " ); 
document.writeln( "cellpadding='6' bordercolor='black'><CAPTION><B>Gas " ); 
document.writeln( "Mileage Calculator</B></CAPTION><THEAD> " ); 
document.writeln( "<TH valign='bottom' align='center'>Gallons</TH> " ); 
document.writeln( "<TH valign='bottom' align='center'>Miles</TH> " ); 
document.writeln( "<TH valign='bottom' align='center'>Miles per<BR>Gallon " ); 
document.writeln( "</TH></THEAD> " ); 

// Get input, calculate miles/gal, output results
for ( tanks=1 ; tanks<11; tanks++ )  {
  gals = parseInt( prompt( "("+tanks+") Enter number of GALLONS (integer, -1 to end input):", "0" ) );
  if( gals == -1 )  {
    tanks--;
    break;
  }
  miles = parseInt( prompt( "Enter number of MILES (integer):", "0" ) );
  per = parseInt( miles/gals );
  avg = avg + per;

  document.writeln( "<TR><TD align='right'>"+gals+"</TD>" ); 
  document.writeln( "<TD align='right'>"+miles+"</TD>" ); 
  document.writeln( "<TD align='right'>"+per+"</TD></TR>" ); 
}

// Output average miles/gal
avg = parseInt( avg / 3 );
document.writeln( "<TR><TD align='right' colspan='2'>Average Miles per " ); 
document.writeln( "Gallon</TD><TD align='right'>"+avg+"</TD></TR></TABLE>");


// end hide -->
</script>
</HEAD>

<BODY>
<P>Click Refresh (or Reload) to run the script again<p>

<a href="9_11src.html">View Source</a><p>

<a href="9_16.html">Exercise 9.16</a>
</BODY>

</HTML>