Need workaround for JavaScript bug I found

wxdqz

New Member
I tested this code in Internet Explorer 6, and Netscape 4.8, and both display the results incorrectly.

Notice I have it make 3 calculations, the first and last are incorrect, while the second is correct. It really confuses me as to why it is displaying incorrect result sometimes, but not others.

Could you guys (and gals) take a look and see if you can tell me what is going wrong?


<HTML>
<HEAD>
<TITLE>JavaScript Test</TITLE>
</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript">
<!--

var kilogram = 1;
var microgram = 1.0e-9;
var nanogram = 1.0e-12;
var picogram = 1.0e-15;

function convert(FromValue, ToValue, Qty)
{
var ConversionFactor = eval(FromValue + "/" + ToValue);
//var ConversionFactor = FromValue / ToValue;
return "" + (Qty * ConversionFactor);
}

document.write("<p>Convert 1 Kilograms to Micrograms:<br>" +
"Correct Answer:<br>1000000000<br>Function returned:<br>" +
convert(kilogram, microgram, 1) + "</p>");
document.write("<p>Convert 1 Kilograms to Nanograms:<br>" +
"Correct Answer:<br>1000000000000<br>Function returned:<br>" +
convert(kilogram, nanogram, 1) + "</p>");
document.write("<p>Convert 1 Kilograms to Picograms:<br>" +
"Correct Answer:<br>1000000000000000<br>Function returned:<br>" +
convert(kilogram, picogram, 1) + "</p>");

//-->
</SCRIPT>

</BODY>
</HTML>
 
Top