Function

Discussion in 'Javascript' started by Philip WATTS, Nov 7, 2003.

  1. Philip WATTS

    Philip WATTS Guest

    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.form1.number1.value
    var no2=document.form1.number2.value

    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>
     
    Philip WATTS, Nov 7, 2003
    #1
    1. Advertising

  2. "Philip WATTS" <> wrote in message
    news:bogrg1$keq$...
    >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.
    >

    <snip>
    > function test1(data1){
    > if (data1>10){
    > return false
    > }
    > }
    >
    > function tests(){
    > var no1=document.form1.number1.value
    > var no2=document.form1.number2.value
    >
    > if (!test1(no1)){

    <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.
     
    Richard Cornford, Nov 7, 2003
    #2
    1. Advertising

  3. "Philip WATTS" <> schreef in bericht
    news:bogrg1$keq$...
    > 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.
    >


    You have forgotton to return true on success:

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


    JW
     
    Janwillem Borleffs, Nov 7, 2003
    #3
  4. Philip WATTS

    Evertjan. Guest

    Janwillem Borleffs wrote on 07 nov 2003 in comp.lang.javascript:
    > "Philip WATTS" <> schreef in bericht
    > news:bogrg1$keq$...
    >> 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.

    >
    > You have forgotton to return true on success:
    >
    > function test1(data1){
    > if (data1>10){
    > return false
    > }
    > return true;
    >}


    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)
     
    Evertjan., Nov 7, 2003
    #4
  5. "Evertjan." <> schreef in bericht
    news:Xns942CDBB6DF3F7eejj99@194.109.133.29...
    >
    > Try:
    >
    > function test1(x){
    > return !(+x > 10)
    > }
    >
    >


    Veel wegen leiden naar Rome...


    JW
     
    Janwillem Borleffs, Nov 8, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,085
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,445
    msalters
    Jan 25, 2005
  3. Replies:
    2
    Views:
    944
    Bengt Richter
    Aug 1, 2005
  4. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,261
    Barry Schwarz
    Sep 5, 2005
  5. weafon
    Replies:
    1
    Views:
    323
    Diez B. Roggisch
    Jul 14, 2009
Loading...

Share This Page