943,682 Members | Top Members by Rank

Ad:
Apr 18th, 2009
0

Unable to print results

Expand Post »
I am doing a small program using a function to print out the real root or imaginary root. The problem is, it only prints out what the numbers that the user input. However, it does not print out the rest.

For Example:

It prints out this:
a = 4 b = 5 c = 3

instead of

a = 4 b = 5 c = 3
root1 = <answer> root2 = <answer>
or
The imaginary number is <answer>


My program is below.

javascript Syntax (Toggle Plain Text)
  1.  
  2. <?xml version = "1.0" encoding = "utf-8"?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  5.  
  6. <html xmlns = "http://www.w3.org/1999/xhtml">
  7.  
  8. <head>
  9.  
  10. <script type = "text/javascript">
  11.  
  12.  
  13. function printRoot(a,b,c)
  14. {
  15. var root1;
  16. var root2;
  17. var radicand = b*b-4.0*a;
  18. var imagpart;
  19. var realpart;
  20.  
  21. if (radicand >= 0.0)
  22. {
  23. root1 = -b+math.sqrt(radicand)/(2.0*a);
  24. root2 = -b-math.sqrt(radicand)/(2.0*a);
  25.  
  26. document.writeln("root1 = "+root1+"root2 = "+root2);
  27. }
  28.  
  29. else
  30. {
  31. realpart = -b/(2.0*a);
  32. imagpart = math.sqrt(math.abs(radicand))/(2.0*a);
  33.  
  34. document.writeln("The imaginary root is"+realpart+" + "+imagpart+"i");
  35. }
  36. }
  37.  
  38.  
  39. var a;
  40. var b;
  41. var c;
  42.  
  43. a = window.prompt("Please enter for a");
  44. b = window.prompt("Please enter for b");
  45. c = window.prompt("Please enter for c");
  46.  
  47.  
  48. while (a != 0.0)
  49. {
  50. document.writeln("a = " + a +" b = " + b + " c = " + c);
  51. printRoot(a,b,c);
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. </script>
  59.  
  60.  
  61.  
  62. </head>
  63.  
  64. <body>
  65.  
  66. </body>
  67.  
  68. </html>
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Apr 18th, 2009
0

Re: Unable to print results

seems fine but a tip is dont use document.writeln.
try having a content area in the page e.g. a "<div>" then give this element and ID and set its innerHTML property to the text you want.

javascript Syntax (Toggle Plain Text)
  1. document.getElementById('someelement').innerHTML += 'your text';
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Chained Selects - using Ajax - can't make 3rd one work
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Problem with JavaScript!!! :(





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


Follow us on Twitter


© 2011 DaniWeb® LLC