Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Variables are being treated as strings??? should be as numbers...

1. ## Variables are being treated as strings??? should be as numbers...

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))
Code:
```<html>
<title>Untitled</title>
<script type="text/javascript">
{
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) {
}
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>
</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>```

• a = form.a.value*1
b = form.b.value*1
c = form.c.value*1
I think that will convert the values to numbers.

• ## Users who have thanked DaveyErwin for this post:

lilwillywonka (09-05-2011)

• Thank you so much!

• Some people regard the *1 trick as a hack. Prefer

Code:
```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.

• Originally Posted by Philip M
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 ...

Code:
```<form>
<input id="a" value="1">
</form>

<script>
</script>```
It seems input elements
within form tags are exempt
from this ie quirk.

• Originally Posted by DaveyErwin
Well in fact his script does work in ie.

check this out ...

Code:
```<form>
<input id="a" value="1">
</form>

<script>
</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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•