By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,760 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,760 IT Pros & Developers. It's quick & easy.

Greater comparison operation

P: n/a
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

--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php
Feb 14 '07 #1
Share this Question
Share on Google+
13 Replies

P: n/a
On Feb 14, 6:59 pm, Otto Wyss <otw...@bluewin.chwrote:
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.

Feb 14 '07 #2

P: n/a
Otto Wyss wrote:
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.

--
Ian Collins.
Feb 14 '07 #3

P: n/a
Ian Collins wrote:
Otto Wyss wrote:
>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)
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

--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php
Feb 14 '07 #4

P: n/a
VK
On Feb 15, 12:20 am, Otto Wyss <otw...@bluewin.chwrote:
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() ) {
}
:-)

Feb 14 '07 #5

P: n/a
Otto Wyss wrote:
I'm probably going to dislike Javascript if I have to use it more.
If you insist on programming in ignorance I would expect you to dislike it a
lot. Programming is a profession that requires deep and specific knowledge. It
can't be done well if you don't know what you are doing.

This may help: http://javascript.crockford.com/survey.html
Feb 15 '07 #6

P: n/a
Douglas Crockford scribed:
>Otto Wyss wrote:
>I'm probably going to dislike Javascript if I have to use it more.

If you insist on programming in ignorance I would expect you to dislike it a
lot. Programming is a profession that requires deep and specific knowledge. It
can't be done well if you don't know what you are doing.

This may help: http://javascript.crockford.com/survey.html
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?
--
Ed Jay (remove 'M' to respond by email)
Feb 15 '07 #7

P: n/a
On Feb 15, 8:04 am, "VK" <schools_r...@yahoo.comwrote:
On Feb 15, 12:20 am, Otto Wyss <otw...@bluewin.chwrote:
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()) {

}
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. :-)
--
Rob

Feb 15 '07 #8

P: n/a
Ed Jay wrote:
Douglas Crockford scribed:

>>Otto Wyss wrote:

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

If you insist on programming in ignorance I would expect you to dislike it a
lot. Programming is a profession that requires deep and specific knowledge. It
can't be done well if you don't know what you are doing.

This may help: http://javascript.crockford.com/survey.html


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.

--
Ian Collins.
Feb 15 '07 #9

P: n/a
Ian Collins scribed:
>Ed Jay wrote:
>Douglas Crockford scribed:

>>>Otto Wyss wrote:
I'm probably going to dislike Javascript if I have to use it more.

If you insist on programming in ignorance I would expect you to dislike it a
lot. Programming is a profession that requires deep and specific knowledge. It
can't be done well if you don't know what you are doing.

This may help: http://javascript.crockford.com/survey.html


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.
Thanks.
--
Ed Jay (remove 'M' to respond by email)
Feb 15 '07 #10

P: n/a
RobG wrote:
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
--
Cross-platform applications:
http://wyoguide.sf.net/index.php?page=projectlist.php
Feb 20 '07 #11

P: n/a
In comp.lang.javascript message <11*********************@p10g2000cwp.goo
glegroups.com>, Wed, 14 Feb 2007 18:43:33, RobG <rg***@iinet.net.au>
posted:
>
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?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 20 '07 #12

P: n/a
Dr J R Stockton wrote on 21 feb 2007 in comp.lang.javascript:
In comp.lang.javascript message <11*********************@p10g2000cwp.goo
glegroups.com>, Wed, 14 Feb 2007 18:43:33, RobG <rg***@iinet.net.au>
posted:
>>
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?
I strongly agree.

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 21 '07 #13

P: n/a
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Wed, 21 Feb 2007 08:08:32, Evertjan. <ex**************@interxnl.net>
posted:
>
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 +.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Feb 21 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.