944,117 Members | Top Members by Rank

Ad:
Nov 4th, 2009
0

Please help!!! JavaScript!

Expand Post »
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?

html Syntax (Toggle Plain Text)
  1. <!-- 2005/11/08 Science Buddies: JavaScript calculator, adds two numbers -->
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Simple Adder</TITLE>
  5.  
  6. <!-- saved from url=(0030)[url]http://www.sciencebuddies.org/[/url] -->
  7. <!-- 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). -->
  8. <SCRIPT LANGUAGE="JavaScript">
  9. <!-- old browsers can't handle JavaScript functions so comment them out
  10. // This is a single-line JavaScript comment.
  11. // Below is a multi-line JavaScript comment.
  12. /* CalculateSum: this function has 3 arguments:
  13.   Atext, Btext and form. It converts Atext and Btext to
  14.   numbers using the built-in JavaScript "parseFloat" method.
  15.   It then uses the form argument to output the sum of the
  16.   numbers to the form's Answer field. Notice that the
  17.   function does *not* need to know the the names of the
  18.   form's input fields. Those values are passed as arguments.
  19.   It does need to know that the form has a field named
  20.   "Answer" so that it can put the result there.
  21.  
  22.   Here is how to end a multi-line JavaScript comment: */
  23.  
  24. function CalculateSum(Atext, Btext, Ctext, Dtext, form)
  25. {
  26. var A = parseFloat(Atext);
  27. var B = parseFloat(Btext);
  28. var C = parseFloat(Ctext);
  29. var D = parseFloat(Ctext);
  30. form.Answer.value = (A * B / C * D);
  31. }
  32.  
  33. /* ClearForm: this function has 1 argument: form.
  34.   It clears the input and answer fields on the form.
  35.   It needs to know the names of the INPUT elements in order
  36.   to do this. */
  37.  
  38. function ClearForm(form)
  39. {
  40. form.input_A.value = "";
  41. form.input_B.value = "";
  42. form.input_C.value = "";
  43. form.input_D.value = "";
  44. form.Answer.value = "";
  45. }
  46.  
  47. // end of JavaScript functions -->
  48. </SCRIPT>
  49. </HEAD>
  50.  
  51. <BODY>
  52.  
  53. <P><FONT SIZE="+2">Simple Adder</FONT></P>
  54.  
  55. <FORM NAME="Calculator" METHOD="post">
  56. <P>Enter the number of SCR trucks you operate: <INPUT TYPE=TEXT NAME="input_A" SIZE=10></P>
  57. <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>
  58. <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>
  59. <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>
  60. <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>
  61. <P><INPUT TYPE="button" VALUE="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></P>
  62. <P>Answer = <INPUT TYPE=TEXT NAME="Answer" SIZE=12></P>
  63. </FORM>
  64.  
  65. </BODY>
  66. </HTML>
  67.  
  68. </html>
Last edited by Ezzaral; Nov 5th, 2009 at 11:59 am. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcrawford3911 is offline Offline
6 posts
since Nov 2009
Nov 5th, 2009
0
Re: Please help!!! JavaScript!
html tags should really be lower case, values quoted, corrected
html Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Simpler Adder</title>
  4. <script type='text/javascript'> // corrected
  5. <!--
  6. function CalculateSum(Atext, Btext, Ctext, Dtext, form)
  7. {
  8. var A = parseFloat(Atext);
  9. var B = parseFloat(Btext);
  10. var C = parseFloat(Ctext);
  11. var D = parseFloat(Dtext); // Error Ctext declared
  12. form.Answer.value = (A * B / C * D);
  13. }
  14. function ClearForm(form)
  15. {
  16. form.input_A.value = "";
  17. form.input_B.value = "";
  18. form.input_C.value = "";
  19. form.input_D.value = "";
  20. form.Answer.value = "";
  21. }
  22. --></script></head>
  23. <body>
  24. <p><font size="+2">Simple Adder</font></p>
  25. <form name="Calculator" method="post">
  26. <p>Enter the number of SCR trucks you operate: <input type='text' name="input_A" size='10'></p>
  27. <p>Enter the average miles for a single truck, traveled in one year: <input type='text' name="input_B" size='10' value='100000'></p>
  28. <p>Enter the average fuel mileage of your trucks (measured as MPG): <input type='text' name="input_C" size='10' value='6'></p>
  29. <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>
  30. <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>
  31. <p><input type='button' value="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></p>
  32. <p>Answer = <input type='text' name="Answer" size='12' readonly></p>
  33. </form>
  34. </body>
  35. </html>
Last edited by almostbob; Nov 5th, 2009 at 12:53 am.
Reputation Points: 562
Solved Threads: 369
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Nov 5th, 2009
0

Thank you!

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

Click to Expand / Collapse  Quote originally posted by almostbob ...
html tags should really be lower case, values quoted, corrected
html Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Simpler Adder</title>
  4. <script type='text/javascript'> // corrected
  5. <!--
  6. function CalculateSum(Atext, Btext, Ctext, Dtext, form)
  7. {
  8. var A = parseFloat(Atext);
  9. var B = parseFloat(Btext);
  10. var C = parseFloat(Ctext);
  11. var D = parseFloat(Dtext); // Error Ctext declared
  12. form.Answer.value = (A * B / C * D);
  13. }
  14. function ClearForm(form)
  15. {
  16. form.input_A.value = "";
  17. form.input_B.value = "";
  18. form.input_C.value = "";
  19. form.input_D.value = "";
  20. form.Answer.value = "";
  21. }
  22. --></script></head>
  23. <body>
  24. <p><font size="+2">Simple Adder</font></p>
  25. <form name="Calculator" method="post">
  26. <p>Enter the number of SCR trucks you operate: <input type='text' name="input_A" size='10'></p>
  27. <p>Enter the average miles for a single truck, traveled in one year: <input type='text' name="input_B" size='10' value='100000'></p>
  28. <p>Enter the average fuel mileage of your trucks (measured as MPG): <input type='text' name="input_C" size='10' value='6'></p>
  29. <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>
  30. <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>
  31. <p><input type='button' value="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></p>
  32. <p>Answer = <input type='text' name="Answer" size='12' readonly></p>
  33. </form>
  34. </body>
  35. </html>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcrawford3911 is offline Offline
6 posts
since Nov 2009
Nov 5th, 2009
0
Re: Please help!!! JavaScript!
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
btw there is a button called reset
check <input type='reset'>
Last edited by futingkiller; Nov 5th, 2009 at 11:55 am.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
futingkiller is offline Offline
17 posts
since Oct 2009
Nov 5th, 2009
0

thank you!

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
btw there is a button called reset
check <input type='reset'>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcrawford3911 is offline Offline
6 posts
since Nov 2009
Nov 5th, 2009
-1
Re: Please help!!! JavaScript!
html Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Simpler Adder</title>
  4. <script type='text/javascript'> // corrected
  5. <!--
  6.   function CalculateSum(Atext, Btext, Ctext, Dtext, Etext, form)
  7.   {
  8.   var A = parseFloat(Atext);
  9.  
  10.   var B = parseFloat(Btext);
  11.  
  12.   var C = parseFloat(Ctext);
  13.  
  14.   var D = parseFloat(Dtext); // Error Ctext declared
  15. var E=parseFloat(Etext);
  16. if( E==300)
  17. form.Answer.value="E is equal to 300";
  18. if(E >
  19. =300)
  20. form.Answer.value="E is greater than 300";
  21. if(E <=300)
  22. form.Answer.value="E is smaller than 300";
  23. if( E==0)
  24. form.Answer.value = (A * B / C * D);
  25.  
  26. }
  27.  
  28. function ClearForm(form)
  29.  
  30. {
  31.  
  32. form.input_A.value = "";
  33.  
  34. form.input_B.value = "";
  35.  
  36. form.input_C.value = "";
  37.  
  38. form.input_D.value = "";
  39.  
  40. form.Answer.value = "";
  41.  
  42. }
  43.  
  44. --></script></head>
  45.  
  46. <body>
  47.  
  48. <p><font size="+2">Simple Adder</font></p>
  49.  
  50. <form name="Calculator" method="post">
  51.  
  52. <p>Enter the number of SCR trucks you operate: <input type='text' name="input_A" size='10'></p>
  53.  
  54. <p>Enter the average miles for a single truck, traveled in one year: <input type='text' name="input_B" size='10' value='100000'></p>
  55.  
  56. <p>Enter the average fuel mileage of your trucks (measured as MPG): <input type='text' name="input_C" size='10' value='6'></p>
  57.  
  58. <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>
  59. <p>What that E stands for:<input type=text name="input_E" size=10 value="default value for E"></p>
  60. <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>
  61.  
  62. <p><input type='reset' value="Clear Fields" name="ClearButton"></p>
  63.  
  64. <p>Answer = <input type='text' name="Answer" size='12' readonly></p>
  65.  
  66. </form>
  67.  
  68. </body>
  69.  
  70. </html>
Last edited by peter_budo; Nov 5th, 2009 at 7:13 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 2
Newbie Poster
futingkiller is offline Offline
17 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: scrollTo(x.y)
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Redirect to page??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC