Velocity Reviews > Function

# Function

Philip WATTS
Guest
Posts: n/a

 11-07-2003
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)){
}else{
}
}

</script>
<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
Guest
Posts: n/a

 11-07-2003
"Philip WATTS" <(E-Mail Removed)> wrote in message
news:bogrg1\$keq\$(E-Mail Removed)...
>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.

Janwillem Borleffs
Guest
Posts: n/a

 11-07-2003

"Philip WATTS" <(E-Mail Removed)> schreef in bericht
news:bogrg1\$keq\$(E-Mail Removed)...
> 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

Evertjan.
Guest
Posts: n/a

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

Janwillem Borleffs
Guest
Posts: n/a

 11-08-2003

"Evertjan." <(E-Mail Removed)> schreef in bericht
news:Xns942CDBB6DF3F7eejj99@194.109.133.29...
>
> Try:
>
> function test1(x){
> return !(+x > 10)
> }
>
>

Veel wegen leiden naar Rome...

JW