Greater comparison operation

O

Otto Wyss

I've this comparison with two numbers

if (t.minimum.value > t.maximum.value) ...

yet it's always true except when minimum and maximum are identical. Why
isn't it false when minimum is smaller than maximum?

O. Wyss
 
R

Richard Cornford

I've this comparison with two numbers

if (t.minimum.value > t.maximum.value) ...

yet it's always true except when minimum and maximum are identical.
Why isn't it false when minimum is smaller than maximum?

The odds are that your 'numbers' are actually strings and your
comparison is lexical.

Richard.
 
I

Ian Collins

Otto said:
I've this comparison with two numbers

if (t.minimum.value > t.maximum.value) ...

yet it's always true except when minimum and maximum are identical. Why
isn't it false when minimum is smaller than maximum?
Try

if (+t.minimum.value > +t.maximum.value)

If there is a chance the values are strings and not numbers.
 
O

Otto Wyss

Ian said:
Try

if (+t.minimum.value > +t.maximum.value)
I can't believe it, yet it works!
If there is a chance the values are strings and not numbers.
I'm probably going to dislike Javascript if I have to use it more.

O. Wyss
 
V

VK

I'm probably going to dislike Javascript if I have to use it more.

So what will you program your pages then? On C#? :)

If the solution by Ian Collins is too "babish" for you then you may
use "light Java" way:

if (new Number(t.minimum.value).valueOf() > new
Number(t.maximum.value).valueOf()) {
}

For full satisfaction you may try "hardcore Java" way as well:

function trutherize(n1, n2) {
return new Boolean(n1.valueOf() > n2.valueOf());
}

function Numerizer(v) {
return new Number(v);
}

if ( trutherize(new Numerizer(t.minimum.value), new
Numerizer(t.maximum.value)).valueOf() ) {
}


:)
 
R

RobG

So what will you program your pages then? On C#? :)

If the solution by Ian Collins is too "babish" for you then you may
use "light Java" way:

if (new Number(t.minimum.value).valueOf() > new
Number(t.maximum.value).valueOf()) {

}

It might have been better to suggest:

if (Number(t.minimum.value) > Number(t.maximum.value)) {


Given that some consider it to be less confusing and more maintainable
than using unary + for type conversion.

For full satisfaction you may try "hardcore Java" way as well:

function trutherize(n1, n2) {
return new Boolean(n1.valueOf() > n2.valueOf());

}

Which is completely unnecessary: where the evaluation uses < or > a
boolean is always returned:

alert( typeof (5 > 6) ); // shows boolean


Similarly for some other comparison operators, but not && or ||
('guard' and 'default' in Crocksford-speak) which return the value of
one of the operand expressions, which *might* be boolean. :)
 
I

Ian Collins

Ed said:
Douglas Crockford scribed:




Within this site it states, "JavaScript shares C-family syntax with Java.."

I know a bit of javascript, but to my prior knowledge I know nothing of C.
Are they syntactically identical?

No, but they are similar.
 
O

Otto Wyss

RobG said:
It might have been better to suggest:

if (Number(t.minimum.value) > Number(t.maximum.value)) {


Given that some consider it to be less confusing and more maintainable
than using unary + for type conversion.
Yes, that's IMO much better since it's obvious what's meant. Thanks.

O. Wyss
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
It might have been better to suggest:

if (Number(t.minimum.value) > Number(t.maximum.value)) {

Given that some consider it to be less confusing and more maintainable
than using unary + for type conversion.


Number is, however, several times slower than + in IE6 - maybe because
it *requires* the creation of two Objects. If the OP is just checking
input control fields, that will not matter; if the code is used in a
large Sort, it might.


Strangely, it seems that this use of unary + was not obvious /a priori/;
but, /a posteriori/, when one reads it in working code, what else could
it be for?
 
E

Evertjan.

Dr J R Stockton wrote on 21 feb 2007 in comp.lang.javascript:
In comp.lang.javascript message <[email protected]



Number is, however, several times slower than + in IE6 - maybe because
it *requires* the creation of two Objects. If the OP is just checking
input control fields, that will not matter; if the code is used in a
large Sort, it might.


Strangely, it seems that this use of unary + was not obvious /a priori/;
but, /a posteriori/, when one reads it in working code, what else could
it be for?

I strongly agree.

The unary + has no other practical function, or do I miss something?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
The unary + has no other practical function, or do I miss something?

It may not have any other function within hand-composed source.

Well, that's not quite true - as well as converting a numeric String to
Number, it will also convert a Boolean. In fact, I used it on Monday,
in adding to js-anclk.htm, like (of course, there's at least one other
reasonable way to code that selection) :

var OO = ["OFF", "ON"]

OK = T>=5 && T<15 // adapt
dg.innerText = OO[+OK]


Then one should be able to enter both +3*3 and -3*3 in a control
accepting expressions, to be read by such as
function UserIn(Ctrl) { return +eval(Ctrl.value) /* for exprns */ }
The unary + in the function removes all possible doubt about the result
being a Number, as before; but one wants unary + to be allowable in the
argument of eval().

Use of eval for JSON input probably needs unary +.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top