Unable to print results

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

Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Unable to print results

 
0
  #1
Apr 18th, 2009
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.

  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>
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Unable to print results

 
0
  #2
Apr 18th, 2009
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.

  1. document.getElementById('someelement').innerHTML += 'your text';
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC