Script Categories













Calculators >>> Miles Per Gallon.

Computes the miles per gallon of your car via the miles traveled and the number of gallons used. Also, if you enter the cost per gallon and how many miles you drive a day, it will estimate your monthly and yearly gas expenses.

Miles Driven: mi.
Gallons of Gas: gal
Cost per Gallon: $
Miles per Day:
~ Mi. per Gal.
$ per month
$ per year

Add the below code to the <body> section of your page:

<script language="javascript" type="text/javascript">
/* Visit http://www.yaldex.com/ for full source code
and get more free JavaScript, CSS and DHTML scripts! */
<!-- Begin
function compute(form) {
var milesPerMo = form.avgMiles.value *30.4;
var mpgcalc = (form.milesDriven.value / form.gallons.value) * 1;
var moGascalc = (((form.avgMiles.value * 30.4) / mpgcalc) * form.ppGal.value) * 1;
mpgcalc = twoDecs(mpgcalc);
moGascalc = twoDecs(moGascalc);
form.mpg.value = mpgcalc;
form.moGas.value = moGascalc;
form.yrGas.value = moGascalc * 12;
}
function twoDecs(item) {
return eval(parseInt(item * 100) * .01);
}
//  End -->
</script>
<form>
<table border=1>
<tr><td>
Miles Driven: </td><td><input type=text name=milesDriven size=5> mi.</td></tr>
<tr><td>
Gallons of Gas: </td><td><input type=text name=gallons size=5> gal</td></tr>
<tr><td>
Cost per Gallon: </td><td>$ <input type=text name=ppGal size=5></td></tr>
<tr><td>
Miles per Day: </td><td><input type=text name=avgMiles size=5></td></tr>
<tr><td
colspan=2 align=center><input type=button value="Calculates To...." onClick="compute(this.form)"></td></tr>
<tr><td>
~ <input type=text name=mpg size=7></td><td>Mi. per Gal.</td></tr>
<tr><td>
$ <input type=text name=moGas size=7></td><td> per month</td></tr>
<tr><td>
$ <input type=text name=yrGas size=7></td><td> per year</td></tr>
<tr>
<td
colspan=2 align=center><input type=reset></td></tr>
</table>
</form>

JavaScript Editor Get Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.



Code was highlighted by 1st JavaScript Editor (The Best JavaScript Editor!).

R7


©