Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jul 2005
Posts: 41
Reputation: jasondrey13 is an unknown quantity at this point 
Solved Threads: 0
jasondrey13's Avatar
jasondrey13 jasondrey13 is offline Offline
Light Poster

debugging?

 
0
  #1
Mar 9th, 2008
hey im really new with javascript.
can someone tell me whats wrong with this?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  5. <title>calculate distance and slope between two points</title>
  6.  
  7. <script language="javascript">
  8. <!--
  9.  
  10. var x1=0;
  11. var y1=0;
  12. var x2=0;
  13. var y2=0;
  14. var xdif=0;
  15. var ydif=0;
  16. var xsldif=0;
  17. var xdifsquared=0;
  18. var ydifsquared=0;
  19. var undersquare=0;
  20. var distance=0;
  21. var slope=0;
  22.  
  23. function calculate()
  24. {
  25.  
  26. x1=document.calcform.inpx1.value;
  27. y1=document.calcform.inpy1.value;
  28. x2=document.calcform.inpx2.value;
  29. y2=document.calcform.inpy2.value;
  30.  
  31. xdif=x2-x1;
  32. ydif=y2-y1;
  33.  
  34. xdifsquared=xdif*xdif;
  35. ydifsquared=ydif*ydif;
  36.  
  37. undersquare=xdifsquared+ydifsquared;
  38.  
  39. distance=Math.sqrt(undersquare);
  40. slope=ydif/xdif;
  41.  
  42. //-->
  43. }
  44.  
  45.  
  46. </script>
  47. </head>
  48. <body>
  49.  
  50. <p><img src="distanceformula.gif" border="1"> </p>
  51. <p><img src="slope.gif" border="1"></p>
  52. <form name="calcform">
  53. <p>point 1: (
  54. <input type="text" name="inpx1" size="5">
  55. ,
  56. <input type="text" name="inpy1" size="5">
  57. ) <br>
  58. point 2: (
  59. <input type="text" name="inpx2" size="5">
  60. ,
  61. <input type="text" name="inpy2" size="5">
  62. )<br>
  63. </p>
  64. <button type="submit" onClick="calculate();">Calculate!</button>
  65. <p>
  66. <strong>distance:</strong>
  67.  
  68. <script language="javascript">document.write(distance);</script>
  69.  
  70. </p>
  71. <p><strong>slope:</strong><br>
  72. <script language="javascript">document.write(slopetop);</script>
  73.  
  74. <br>
  75. ----- = <script language="javascript">document.write(slopefinal);</script><br>
  76.  
  77. <script language="javascript">document.write(slopebottom);</script>
  78. <br>
  79. </p>
  80. </form>
  81.  
  82.  
  83.  
  84.  
  85. </body>
  86. </html>


thanks so much,
Jason
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: debugging?

 
1
  #2
Mar 9th, 2008
Where have you declared 'slopetop', 'slopebottom' and 'slopefinal'? And document.write() is evaluated as the page is being rendered. Plus Javascript is a client side technology; as soon as a page is submitted, what you get is a new page and the state maintained in Javascript variables is reset.

You need to dynamically update the value of a span or div element to get this code working as expected. Something like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <meta http-equiv="Expires" content="0" /> <!-- disable caching -->
  7. <title>Example</title>
  8. <script type="text/javascript">
  9. function calculate(frm) {
  10. if(!frm)
  11. return(false);
  12. var elems = frm.elements;
  13. var x1 = parseInt(elems['inpx1'].value, 10);
  14. var x2 = parseInt(elems['inpx2'].value, 10);
  15. var y1 = parseInt(elems['inpy1'].value, 10);
  16. var y2 = parseInt(elems['inpy2'].value, 10);
  17. var e = elems['result1'];
  18. alert("(" + x1 + "," + y1 + ")" + " and (" + x2 + "," + y2 + ")");
  19. var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  20. e.value = isNaN(distance) ? 'Invalid input' : distance;
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <form action="#">
  26. <div>(<input type="text" name="inpx1" size="5">,<input type="text" name="inpy1" size="5">)</div>
  27. <br>
  28. <div>(<input type="text" name="inpx2" size="5">,<input type="text" name="inpy2" size="5">)</div>
  29. <br>
  30. <input type="button" onclick="return calculate(this.form);" value="Calculate!">
  31. <br><br>
  32. <div>Distance: <input name="result1" readonly="readonly"></div>
  33. </form>
  34. </body>
  35. </html>
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 41
Reputation: jasondrey13 is an unknown quantity at this point 
Solved Threads: 0
jasondrey13's Avatar
jasondrey13 jasondrey13 is offline Offline
Light Poster

Re: debugging?

 
0
  #3
Mar 9th, 2008
thanks!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: debugging?

 
0
  #4
Mar 9th, 2008
You were having a hard time debugging this simple snippet because maybe you were testing this code in IE; for debugging use Firefox. It provides good debugging support given it has error console (Tools -> Error Console). To try it out, run your previous snippet which you just posted in Firefox and look at the error console; the errors would be right there.

If you are up to it, you can even try the Firebug plugin which provides full blown debugging. And you can be pretty sure that if something works in Firefox will also work in IE.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC