Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2013, 01:27 PM   PM User | #1
mos5
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mos5 is an unknown quantity at this point
Exclamation Script from HTML form help required

Hello,

I am trying to create a javascript function (Please see the function below) which calculates the total cost of a trip for three different vehicles based on html form input (Please see HTML code below) but I cannot get the script to work. Any help would be appreciated.

Here is the HTML code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" type="text/css" href=".css" />
<!-- Alternate Netscape 4 shielding version of including css file
<style type="text/css">@import "C:\Users\ME\Desktop\.css";</style>
-->
<script type="text/javascript" src="C:\Users\ME\Desktop\Lab3.js">
</script>
<title>Calculating with JavaScript and HTML Forms</title>
</head>
<body>
<noscript>
<h2>This page requires JavaScript</h2>
</noscript>

<img src="C:\Users\ME\Desktop\logoSML.bmp" alt="logo" width="400" />
<p> In this lab lets look at some additional things that JavaScript
can do, such as make calculations with user input from a form. </p>

<h2>The Problem: </h2>

<p>I want to take a drive from Colorado Springs to
Yellowstone Park. The reported distance is 638 miles. I would like to know
how much the fuel is going to cost me to make this round trip. I have three options
for my transportation....Assume that gas costs on averate $2.33 per gallon.</p>

<ol>
<li>My Car that gets on average 19mpg...</li>
<li>My Motorcycle that gets on average 76 mpg...</li>
<li>My Friends hybrid that gets on average 48mpg</li>
</ol>
<br />

<p> Fill in the form items below to get the input necessary to make the calculations: </p>

<p> <label>Distance to be traveled: <input type="text" size = "8" name="distance"
id="dist" /></label></p>
<p> <label>MPG for My Car: <input type="text" size = "8" name="mycarMpg"
id="mycarMPG" /></label></p>
<p> <label>MPG for Motorcycle: <input type="text" size = "8" name="mtrcycMpg"
id="mcmpg" /></label></p>
<p> <label>MPG for the Hybrid: <input type="text" size = "8" name="hybridMpg"
id="hmpg" /></label></p>

<div>
<input type="button" value="Calculate the cost for each vehicle"
onclick="CalculateCost();"/>
</div>

<br /> <br />
<table align="center" border="3">
<tr>
<th>Cost with My Car</th>
<th>Cost with Motorcycle</th>
<th>Cost with Hybrid</th>
</tr>
<tr>
<td id="mycar">&nbsp</td>
<td id="mtrcyc">&nbsp</td>
<td id="hybrid">&nbsp</td>
</tr>
</table>

</body>
</html>

Here is my attempt at the script:

function CalculateCost(){
var dist = document.getElementById("dist").value;
var mycarMpg = document.getElementById("mycarMpg").value;
var mtrcycMPG = document.getElementById("mtrcycMpg").value;
var hybridMpg = document.getElementById("hybridMpg").value;

var mycar = document.getElementById("mycar").value;
var mtrcyc = document.getElementById("mtrcyc").value;
var hybrid = document.getElementById("hybrid").value;

mycar = dist/mycarMpg*2.33;
mycar = Math.round(mycar * 100)/100;

mtrcyc = dist/mtrcycMPG*2.33;
mtrcyc = Math.round(mtrcyc * 100)/100;

hybrid = dist/hybridMpg*2.33;
hybrid = Math.round(hybrid * 100)/100;
}

Last edited by mos5; 01-20-2013 at 03:03 PM.. Reason: Personal information
mos5 is offline   Reply With Quote
Old 01-22-2013, 12:19 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,362
Thanks: 18
Thanked 347 Times in 346 Posts
sunfighter is on a distinguished road
Code:
var mtrcycMPG = document.getElementById("mtrcycMpg").value;
var hybridMpg = document.getElementById("hybridMpg").value;
Should Be:
Code:
var mtrcycMPG = document.getElementById("mcmpg").value;
var hybridMpg = document.getElementById("hmpg").value;
Your IDs.
---------------------------------------------------------------------

These are not needed. It's where the answers go.
Code:
var mycar = document.getElementById("mycar").value;
var mtrcyc = document.getElementById("mtrcyc").value;
var hybrid = document.getElementById("hybrid").value;
Your math is wrong. You multiply first than divide. So your missing parentheses (). It's mycar = (dist/mycarMpg)*2.33;
Save some time and use (dist*2.33)/mycarMpg. (dist*2.33) is a constent and can be used in all calculations.

K = 638 * 2.33 = 1,486.54
so mycar = 1,486.54/mycarMpg;same for mtrcyc and hybrid.

And put the lines in to write your output.
sunfighter is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:01 PM.


Advertisement
Log in to turn off these ads.