0

i’m using javascript, below is the form. If you go to the(A * B / C * D); part of the form. For some reason it does not work…do not know if I have something wrong. But if I were to enter 30 as A, 100,000 as B, 6 as C, and .2 as D. The answer should be 10,000. Can you help me figure out what is wrong?

<!-- 2005/11/08 Science Buddies: JavaScript calculator, adds two numbers -->
<HTML>
<HEAD>
<TITLE>Simple Adder</TITLE>

<!-- saved from url=(0030)[url]http://www.sciencebuddies.org/[/url] -->
<!-- When this code is saved as a local file, the preceding line tells Internet Explorer to treat this file according to the security rules for the Internet zone (plus any security rules specific for the Science Buddies website). -->
<SCRIPT LANGUAGE="JavaScript">
<!-- old browsers can't handle JavaScript functions so comment them out
// This is a single-line JavaScript comment.
// Below is a multi-line JavaScript comment.
/* CalculateSum: this function has 3 arguments:
   Atext, Btext and form. It converts Atext and Btext to 
   numbers using the built-in JavaScript "parseFloat" method. 
   It then uses the form argument to output the sum of the 
   numbers to the form's Answer field. Notice that the 
   function does *not* need to know the the names of the 
   form's input fields. Those values are passed as arguments. 
   It does need to know that the form has a field named 
   "Answer" so that it can put the result there.

   Here is how to end a multi-line JavaScript comment: */

function CalculateSum(Atext, Btext, Ctext, Dtext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var D = parseFloat(Ctext);
form.Answer.value = (A * B / C * D);
}

/* ClearForm: this function has 1 argument: form.
   It clears the input and answer fields on the form. 
   It needs to know the names of the INPUT elements in order
   to do this. */

function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.input_C.value = "";
form.input_D.value = "";
form.Answer.value = "";
}

// end of JavaScript functions -->
</SCRIPT>
</HEAD>

<BODY>

<P><FONT SIZE="+2">Simple Adder</FONT></P>

<FORM NAME="Calculator" METHOD="post">
<P>Enter the number of SCR trucks you operate: <INPUT TYPE=TEXT NAME="input_A" SIZE=10></P>
<P>Enter the average miles for a single truck, traveled in one year (default is 100,000 miles p.a.): <INPUT TYPE=TEXT NAME="input_B" SIZE=10></P>
<P>Enter the average fuel mileage of your trucks (measured as MPG, defaults is 6 MPG): <INPUT TYPE=TEXT NAME="input_C" SIZE=10></P>
<P>Default dosage rate is 2% DEF per every gallon of diesel fuel based on engine manufactures: <INPUT TYPE=TEXT NAME="input_D" SIZE=10></P>
<P><INPUT TYPE="button" VALUE="Average yearly gallons of DEF you may consume" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form.input_D.value, this.form)"></P>
<P><INPUT TYPE="button" VALUE="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></P>
<P>Answer = <INPUT TYPE=TEXT NAME="Answer" SIZE=12></P>
</FORM>

</BODY>
</HTML>

</html>

Edited by Ezzaral: Added [code] [/code] tags. Please use them to format any code that you post.

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by futingkiller
0

html tags should really be lower case, values quoted, corrected

<html>
<head>
<title>Simpler Adder</title>
<script type='text/javascript'> // corrected
<!--
function CalculateSum(Atext, Btext, Ctext, Dtext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var D = parseFloat(Dtext);               // Error Ctext declared
form.Answer.value = (A * B / C * D);
}
function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.input_C.value = "";
form.input_D.value = "";
form.Answer.value = "";
}
--></script></head>
<body>
<p><font size="+2">Simple Adder</font></p>
<form name="Calculator" method="post">
<p>Enter the number of SCR trucks you operate: <input type='text' name="input_A" size='10'></p>
<p>Enter the average miles for a single truck, traveled in one year: <input type='text' name="input_B" size='10' value='100000'></p>
<p>Enter the average fuel mileage of your trucks (measured as MPG): <input type='text' name="input_C" size='10' value='6'></p>
<p>Default dosage rate is 2% DEF per every gallon of diesel fuel based on engine manufactures: <input type='text' name="input_D" size='10' value='.02'></p>
<p><input type="button" value="Average yearly gallons of DEF you may consume" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form.input_D.value, this.form)"></p>
<p><input type='button' value="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></p>
<p>Answer = <input type='text' name="Answer" size='12' readonly></p>
</form>
</body>
</html>

Edited by almostbob: n/a

0

Thank you, I'm going to look at the code and figure out the difference....I really appreciate it!!!

html tags should really be lower case, values quoted, corrected

<html>
<head>
<title>Simpler Adder</title>
<script type='text/javascript'> // corrected
<!--
function CalculateSum(Atext, Btext, Ctext, Dtext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var D = parseFloat(Dtext);               // Error Ctext declared
form.Answer.value = (A * B / C * D);
}
function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.input_C.value = "";
form.input_D.value = "";
form.Answer.value = "";
}
--></script></head>
<body>
<p><font size="+2">Simple Adder</font></p>
<form name="Calculator" method="post">
<p>Enter the number of SCR trucks you operate: <input type='text' name="input_A" size='10'></p>
<p>Enter the average miles for a single truck, traveled in one year: <input type='text' name="input_B" size='10' value='100000'></p>
<p>Enter the average fuel mileage of your trucks (measured as MPG): <input type='text' name="input_C" size='10' value='6'></p>
<p>Default dosage rate is 2% DEF per every gallon of diesel fuel based on engine manufactures: <input type='text' name="input_D" size='10' value='.02'></p>
<p><input type="button" value="Average yearly gallons of DEF you may consume" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form.input_D.value, this.form)"></p>
<p><input type='button' value="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></p>
<p>Answer = <input type='text' name="Answer" size='12' readonly></p>
</form>
</body>
</html>
0

Thank you, I'm going to look at the code and figure out the difference....I really appreciate it!!!

the different line is:
var D = parseFloat(Dtext);
you copypaste too much :P
btw there is a button called reset :)
check <input type='reset'> :)

Edited by futingkiller: n/a

0

Yes, I'm a huge copy and paster.
Do you know how to add a if statement to this???? I'm just learning and would love some advice.
Need a if statement as follows for the “Recommended form of package to be used when ordering DEF” ad button. E would be the answer from what you just helped with...but do not know how to add E either. Wish there was some kind of tutorial...I love to learn and when I get it myself it is even better. =IF(E<300,Jugs,IF(AND(E>=300,E<800),Drums,IF(AND(E>=800,E<6601),IBC,Mini Bulk)))

the different line is:
var D = parseFloat(Dtext);
you copypaste too much :P
btw there is a button called reset :)
check <input type='reset'> :)

-1
<html>
<head>
<title>Simpler Adder</title>
<script type='text/javascript'> // corrected
<!--
   function CalculateSum(Atext, Btext, Ctext, Dtext, Etext, form)
   {
   var A = parseFloat(Atext);

      var B = parseFloat(Btext);

      var C = parseFloat(Ctext);

      var D = parseFloat(Dtext); // Error Ctext declared
var E=parseFloat(Etext);
if( E==300)
form.Answer.value="E is equal to 300";
if(E >=300)
form.Answer.value="E is greater than 300";
if(E <=300)
form.Answer.value="E is smaller than 300";
if( E==0)
      form.Answer.value = (A * B / C * D);

      }

      function ClearForm(form)

      {

      form.input_A.value = "";

      form.input_B.value = "";

      form.input_C.value = "";

      form.input_D.value = "";

      form.Answer.value = "";

      }

      --></script></head>

      <body>

      <p><font size="+2">Simple Adder</font></p>

      <form name="Calculator" method="post">

      <p>Enter the number of SCR trucks you operate: <input type='text' name="input_A" size='10'></p>

      <p>Enter the average miles for a single truck, traveled in one year: <input type='text' name="input_B" size='10' value='100000'></p>

      <p>Enter the average fuel mileage of your trucks (measured as MPG): <input type='text' name="input_C" size='10' value='6'></p>

      <p>Default dosage rate is 2% DEF per every gallon of diesel fuel based on engine manufactures: <input type='text' name="input_D" size='10' value='.02'></p>
<p>What that E stands for:<input type=text name="input_E" size=10 value="default value for E"></p>
      <p><input type="button" value="Average yearly gallons of DEF you may consume" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form.input_D.value, this.form.input_E.value, this.form)"></p>

      <p><input type='reset' value="Clear Fields" name="ClearButton"></p>

      <p>Answer = <input type='text' name="Answer" size='12' readonly></p>

      </form>

      </body>

      </html>

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.