...

View Full Version : Variables are being treated as strings??? should be as numbers...



lilwillywonka
09-05-2011, 06:56 PM
I believe the problem with my code here is that for some reason, on the equation for VertY.. it is being treated as a string.. I want it to be a math equation, but it treats it as text. (ie -1 + 1 should be 0, but it says it is -11)
I believe this is the problem code:
form.VertY.value = ((a * (form.VertX.value * form.VertX.value)) + (b * form.VertX.value) + (c))


<html>
<head>
<title>Untitled</title>
</head>
<script type="text/javascript">
function quad(form)
{
a = form.a.value
b = form.b.value
c = form.c.value
form.Dis.value = (b * b) - (4 * a * c)
form.VertX.value = (-b) / (2 * a)
form.VertY.value = ((a * (form.VertX.value * form.VertX.value)) + (b * form.VertX.value) + (c))

if (a == 0) {
alert("ERROR, EQUATION MUST BE QUADRATIC")
}
else {
if (form.Dis.value > 0) {
form.NumSol.value = 2;
form.SolOne.value = ((-b) + Math.sqrt(form.Dis.value)) / (2 * a)
form.SolTwo.value = ((-b) - Math.sqrt(form.Dis.value)) / (2 * a)
}
else if (form.Dis.value == 0) {
form.NumSol.value = 1;
if (((-b) - Math.sqrt(form.Dis.value)) == 0) {
form.SolOne.value = ((-b) - Math.sqrt(form.Dis.value)) / (2 * a)
form.SolTwo.value = "NO SOLUTION"
}
else {
form.SolOne.value = ((-b) + Math.sqrt(form.Dis.value)) / (2 * a)
form.SolTwo.value = "NO SOLUTION"
}
}
else {
form.NumSol.value = 0;
form.SolOne.value = "NO SOLUTION"
form.SolTwo.value = "NO SOLUTION"
}
}

if (a > 0) {
form.Open.value = "Up"
}
else {
form.Open.value = "Down"
}


}
</script>

<body>
<form name="form1">
<table border="2" align=center>
<center>
<tr>
<td align=center valign=middle><input type="text" name="a" size=3>
x^2 +

<input type="text" name="b" size=3>
x +

<input type="text" name="c" size=3>
</td>
</tr>
<tr><td align=center valign=middle>
<input type="button" value="Calculate" onClick="quad(this.form)">
</td></tr>

<tr><td align=center valign=middle>Discriminant: <input type="text" name="Dis" size = 7 /></td></tr>
<tr><td align=center valign=middle>Number of Solutions: <input type="text" name="NumSol" size = 2 /></td></tr>

<tr><td align=center valign=middle><b>Solution 1: </b><input type="text" name="SolOne" size = 10 style="font-weight:bold;" /></td></tr>
<tr><td align=center valign=middle><b>Solution 2: </b><input type="text" name="SolTwo" size = 10 style="font-weight:bold;" /></td></tr>

<tr><td align=center valign=middle>Your graph will open: <input type="text" name="Open" size = 4 /></td></tr>

<tr><td align=center valign=middle>

Vertex: ( <input type="text" name="VertX" size = 2 />
, <input type="text" name="VertY" size = 2 /> )

</td></tr>
</table>
</form>
</center>



</body>
</html>

DaveyErwin
09-05-2011, 07:46 PM
a = form.a.value*1
b = form.b.value*1
c = form.c.value*1
I think that will convert the values to numbers.

lilwillywonka
09-05-2011, 07:49 PM
Thank you so much!

Philip M
09-05-2011, 09:35 PM
Some people regard the *1 trick as a hack. Prefer


var av = Number(form.a.value);
var bv = Number(form.b.value);
var cv = Number(form.c.value);

The var keyword makes the variable local in scope, and avoids cluttering the global namespace.

It is not a good idea to use the same name for a Javascript variable and an HTML element. In Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID. Because your variables a, b and c are global your script would not work in IE. Give the script variables a different name (av, bv and cv).

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

DaveyErwin
09-05-2011, 10:18 PM
Because your variables a, b and c are global your script would not work in IE.


Well in fact his script does work in ie.

check this out ...


<form>
<input id="a" value="1">
</form>

<script>
alert(a)
</script>

It seems input elements
within form tags are exempt
from this ie quirk.

Philip M
09-06-2011, 11:19 AM
Well in fact his script does work in ie.

check this out ...


<form>
<input id="a" value="1">
</form>

<script>
alert(a)
</script>

It seems input elements
within form tags are exempt
from this ie quirk.


I have checked it out, and not surprisingly I get "a is undefined".

You can never assume that your code will be the only script used in the document. So it is important that you make sure your code does not have global function or variable names that other scripts can override.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum