Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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>
    <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>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you so much!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by Philip M View Post
    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>
    alert(a)
    </script>
    It seems input elements
    within form tags are exempt
    from this ie quirk.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by DaveyErwin View Post
    Well in fact his script does work in ie.

    check this out ...

    Code:
    <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.
    Last edited by Philip M; 09-06-2011 at 11:35 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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