What's Here?
- Members: 131,945
- Replies: 470,221
- Topics: 72,879
- Snippets: 2,538
- Tutorials: 664
- Total Online: 1,933
- Members: 77
- Guests: 1,856
Who's Online?
|
A function to calculate gas mileage.
|
Submitted By: OliveOyl3471
|
|
Rating:
   
|
|
Views: 253 |
Language: JavaScript
|
|
Last Modified: October 13, 2008 |
|
Instructions: Copy and paste the function into the document head. Copy and paste the form into the document body. |
Snippet
function calcMPG()
{
var startMiles = document.forms[0].startingMileage.value;
var endMiles = document.forms[0].endingMileage.value;
var gallons = document.forms[0].gallonsUsed.value;
if(isNaN(startMiles) || isNaN(endMiles) || isNaN(gallons))
{
window.alert("Each textbox must contain only numbers!");
}
else
{
if(gallons > 0)
{
document.forms[0].milesPerGallon.value = ((endMiles - startMiles)/gallons).toFixed(1);
}
}
}
<body>
<script type="text/javascript">
document.write("<p><h2>Miles Per Gallon Calculator</h2></p>");
document.write("<p><h5>To find miles per gallon, enter starting and ending mileage and gallons of gas used.</h5></p>");
</script>
<form action ="">
<p>Starting Miles:<br />
<input type = "text" name = "startingMileage" value = "0" size = "10" onchange = "calcMPG()" /></p>
<p>Ending Miles:<br />
<input type = "text" name = "endingMileage" value = "0" size = "10" onchange = "calcMPG()" /></p>
<p>Gallons Used:<br />
<input type = "text" name = "gallonsUsed" value = "0" size = "10" onchange = "calcMPG()" /></p>
<p>Miles Per Gallon:<br />
<input type = "text" name = "milesPerGallon" value = "0" size = "10" /></p>
</form>
</body>
Copy & Paste
|
|
|
Programming
Web Development
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|