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.
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<script type = "text/javascript">
function printRoot(a,b,c)
{
var root1;
var root2;
var radicand = b*b-4.0*a;
var imagpart;
var realpart;
if (radicand >= 0.0)
{
root1 = -b+math.sqrt(radicand)/(2.0*a);
root2 = -b-math.sqrt(radicand)/(2.0*a);
document.writeln("root1 = "+root1+"root2 = "+root2);
}
else
{
realpart = -b/(2.0*a);
imagpart = math.sqrt(math.abs(radicand))/(2.0*a);
document.writeln("The imaginary root is"+realpart+" + "+imagpart+"i");
}
}
var a;
var b;
var c;
a = window.prompt("Please enter for a");
b = window.prompt("Please enter for b");
c = window.prompt("Please enter for c");
while (a != 0.0)
{
document.writeln("a = " + a +" b = " + b + " c = " + c);
printRoot(a,b,c);
}
</script>
</head>
<body>
</body>
</html>