Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip WATTS

    Function

    Can anyone tell me why the following script always returns my "failed test"
    result. Surely if my input of "No1" is below 10 I should get the "Test1
    past" message.

    Thanks
    Phil

    function test1(data1){
    if (data1>10){
    return false
    }
    }

    function tests(){
    var no1=document.fo rm1.number1.val ue
    var no2=document.fo rm1.number2.val ue

    if (!test1(no1)){
    alert("test failed"+no1)
    }else{
    alert("test1 passed")
    }
    }

    </script>
    </head>
    <body>
    <form name="form1">
    <input type="text" name="number1">
    <input type="text" name="number2">
    <input type="button" value="test" Onclick="tests( )">

    </form>
    </body>
    </html>


  • Richard Cornford

    #2
    Re: Function

    "Philip WATTS" <PRWATTS@syring a.freeserve.co. uk> wrote in message
    news:bogrg1$keq $1@news8.svr.po l.co.uk...[color=blue]
    >Can anyone tell me why the following script always returns my
    >"failed test" result. Surely if my input of "No1" is below 10
    >I should get the "Test1 past" message.
    >[/color]
    <snip>[color=blue]
    > function test1(data1){
    > if (data1>10){
    > return false
    > }
    > }
    >
    > function tests(){
    > var no1=document.fo rm1.number1.val ue
    > var no2=document.fo rm1.number2.val ue
    >
    > if (!test1(no1)){[/color]
    <snip>

    The value being passed to the function is a string and the comparisons
    will tend to type-convert the number 10 to a string for comparison if
    the data1 value is a string. And string comparison makes "10" smaller
    than "2".

    At some point you need to convert the string acquired from the value
    properties of the form fields into a numeric value if you want to
    compare that number with another.

    <URL: http://jibbering.com/faq/#FAQ4_21 >

    Richard.


    Comment

    • Janwillem Borleffs

      #3
      Re: Function


      "Philip WATTS" <PRWATTS@syring a.freeserve.co. uk> schreef in bericht
      news:bogrg1$keq $1@news8.svr.po l.co.uk...[color=blue]
      > Can anyone tell me why the following script always returns my "failed[/color]
      test"[color=blue]
      > result. Surely if my input of "No1" is below 10 I should get the "Test1
      > past" message.
      >[/color]

      You have forgotton to return true on success:

      function test1(data1){
      if (data1>10){
      return false
      }
      return true;
      }


      JW



      Comment

      • Evertjan.

        #4
        Re: Function

        Janwillem Borleffs wrote on 07 nov 2003 in comp.lang.javas cript:[color=blue]
        > "Philip WATTS" <PRWATTS@syring a.freeserve.co. uk> schreef in bericht
        > news:bogrg1$keq $1@news8.svr.po l.co.uk...[color=green]
        >> Can anyone tell me why the following script always returns my "failed[/color]
        > test"[color=green]
        >> result. Surely if my input of "No1" is below 10 I should get the
        >> "Test1 past" message.[/color]
        >
        > You have forgotton to return true on success:
        >
        > function test1(data1){
        > if (data1>10){
        > return false
        > }
        > return true;
        >}[/color]

        Try:

        function test1(x){
        return !(+x > 10)
        }

        1 force the value to compare numeric

        2 the if-testing of a boolean for extracting false/true is superfluous,
        except in in a tutorial


        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        • Janwillem Borleffs

          #5
          Re: Function


          "Evertjan." <exjxw.hannivoo rt@interxnl.net > schreef in bericht
          news:Xns942CDBB 6DF3F7eejj99@19 4.109.133.29...[color=blue]
          >
          > Try:
          >
          > function test1(x){
          > return !(+x > 10)
          > }
          >
          >[/color]

          Veel wegen leiden naar Rome...


          JW



          Comment

          Working...