SIMPLE NUMBER COMPARISON

W

Window Frog

Hello gurus... I must have looked at this too long and something
simple has stopped making sense. I am trying to compare 2 numbers to
each to check if one is >= to the other, but the weirdest thing keeps
happening. If the number is any more than 1 digit it does not work.
For example, the script sees 12 as being smaller than 7 because (it
seems) it's only comparing the first digit. Below is the function that
is doing the checking. Any thoughts? Thanks all.

<script language="javascript">

function checkTest()
{
var error_string = "";

if (window.document.form.iPrice2.value >=
window.document.form.iPrice1.value)
{
error_string += "NO 1.\n";
}
if (window.document.form.iPrice3.value >=
window.document.form.iPrice2.value)
{
error_string += "NO 2.\n";
}
if (window.document.form.iPrice3.value == "")
{
error_string += "NO 3.\n";
}
if (error_string == "")
{
return true;
} else {
error_string = "Check the form and complete the following:\n" +
error_string;
alert(error_string);
return false;
}
}

</script>
 
M

My Pet Programmer

Window Frog said:
Hello gurus... I must have looked at this too long and something
simple has stopped making sense. I am trying to compare 2 numbers to
each to check if one is >= to the other, but the weirdest thing keeps
happening. If the number is any more than 1 digit it does not work.
For example, the script sees 12 as being smaller than 7 because (it
seems) it's only comparing the first digit. Below is the function that
is doing the checking. Any thoughts? Thanks all.
You're making them strings. Well, more to the point, you're NOT forcing
them to be numeric.
<script language="javascript">

function checkTest()
{
var error_string = "";

if (window.document.form.iPrice2.value >=
window.document.form.iPrice1.value)
{
You want:
var frm = document.forms['form'];
if (+frm['iPrice2'].value >= +frm['iPrice1'].value)

The plus signs tell javascript to go ahead and treat them like numbers.

Because it's comparing the string "12" to "7", and this is what it sees:
"12"
"7-"
"7" > "1", comparison finished.

If you had "70" and "1350000", it would still say 70 was greater.
Because as a string, it is.

~A!


--
Anthony Levensalor
(e-mail address removed)

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
 
W

Window Frog

Window Frog said:> Hello gurus... I must have looked at this too long and something
simple has stopped making sense. I am trying to compare 2 numbers to
each to check if one is >= to the other, but the weirdest thing keeps
happening. If the number is any more than 1 digit it does not work.
For example, the script sees 12 as being smaller than 7 because (it
seems) it's only comparing the first digit. Below is the function that
is doing the checking. Any thoughts? Thanks all.

You're making them strings. Well, more to the point, you're NOT forcing
them to be numeric.
<script language="javascript">
function checkTest()
{
var error_string = "";
if (window.document.form.iPrice2.value >=
window.document.form.iPrice1.value)
{

You want:
var frm = document.forms['form'];
if (+frm['iPrice2'].value >= +frm['iPrice1'].value)

The plus signs tell javascript to go ahead and treat them like numbers.

Because it's comparing the string "12" to "7", and this is what it sees:
"12"
"7-"
"7" > "1", comparison finished.

If you had "70" and "1350000", it would still say 70 was greater.
Because as a string, it is.

~A!

--
Anthony Levensalor
(e-mail address removed)

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein

That is great stuff!!! Thank you. I was tearing my hair out. I think
my eyes are just burning from staring at the project too long and
solutions (even simple ones) are slow in coming.
 
M

My Pet Programmer

Window Frog said:
Window Frog said:> Hello gurus... I must have looked at this too long and something
simple has stopped making sense. I am trying to compare 2 numbers to
each to check if one is >= to the other, but the weirdest thing keeps
happening. If the number is any more than 1 digit it does not work.
For example, the script sees 12 as being smaller than 7 because (it
seems) it's only comparing the first digit. Below is the function that
is doing the checking. Any thoughts? Thanks all.
You're making them strings. Well, more to the point, you're NOT forcing
them to be numeric.
<script language="javascript">
function checkTest()
{
var error_string = "";
if (window.document.form.iPrice2.value >=
window.document.form.iPrice1.value)
{
You want:
var frm = document.forms['form'];
if (+frm['iPrice2'].value >= +frm['iPrice1'].value)

The plus signs tell javascript to go ahead and treat them like numbers.

Because it's comparing the string "12" to "7", and this is what it sees:
"12"
"7-"
"7" > "1", comparison finished.

If you had "70" and "1350000", it would still say 70 was greater.
Because as a string, it is.

~A!

--
Anthony Levensalor
(e-mail address removed)

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein

That is great stuff!!! Thank you. I was tearing my hair out. I think
my eyes are just burning from staring at the project too long and
solutions (even simple ones) are slow in coming.
Nah, that's just a pain in the ass. The first time I went through that I
damn near killed my cat, no worries.

:)

~A!

--
Anthony Levensalor
(e-mail address removed)

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Fri, 28 Dec 2007 09:49:41, My Pet Programmer
You want:
var frm = document.forms['form'];
if (+frm['iPrice2'].value >= +frm['iPrice1'].value)

When giving such answers, one should cite the newsgroup FAQ. That will
encourage the consultation of the FAQ for other questions.

The answer to the OP's need is contained in FAQ 4.21.


<FAQENTRY> Extend 4.21 to refer also to the use of relational operators.

Ideally, rewrite the whole section. The essential beginning of the
situation, from the point of view of those who need to be helped by it,
is that the .value property of a control is always a string. Having
established that, one can then refer to how strings are compared and
'plus'ed, leading to the need to convert them to forms for which an
available operator performs the desired sort of operation.

IMHO, while MPP's answer is technically OK :

(1) it's better to recommend the form
var price1 = + frm.['iPrice1'].value
or var price1 = + frm.iPrice1.value
in which the value is more obviously converted at first input and stored
in a variable for possibly-multiple use,

(2) String comparison is better described as character-by-character,
with the "missing" later characters of a shorter string being treated as
less than any possible non-missing character.

That suggests a possible though improbable bug to test for - the string
"A" should compare less than the string "A\u0000".


Warning : .toString() of a function is not defined. Nevertheless, one
might not expect that, whereas for IE6 and FF2 the result starts with
the characters f u n , in Opera 9.25 there is a preceding LF character
(number ten).
 
T

Thomas 'PointedEars' Lahn

Dr said:
Warning : .toString() of a function is not defined.

Yes, it is:

,-[ECMAScript Ed. 3 Final]
|
| 15.3.4.2 Function.prototype.toString ( )
|
| An implementation-dependent representation of the function is returned.
| This representation has the syntax of a FunctionDeclaration. Note in
| particular that the use and placement of white space, line terminators,
| and semicolons within the representation string is
| implementation-dependent.
|
| The toString function is not generic; it throws a TypeError exception if
| its this value is not a Function object. Therefore, it cannot be
| transferred to other kinds of objects for use as a method.
Nevertheless, one might not expect that, whereas for IE6 and FF2 the
result starts with the characters f u n , in Opera 9.25 there is a
preceding LF character (number ten).

That behavior is ECMAScript-compliant and should not be unexpected after
reading the above section of the Specification.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Sun,
30 Dec 2007 02:11:42 said:
Dr said:
Warning : .toString() of a function is not defined.

Yes, it is:

,-[ECMAScript Ed. 3 Final]
|
| 15.3.4.2 Function.prototype.toString ( )
|
| An implementation-dependent representation of the function is returned.
| This representation has the syntax of a FunctionDeclaration. Note in
| particular that the use and placement of white space, line terminators,
| and semicolons within the representation string is
| implementation-dependent.
|
| The toString function is not generic; it throws a TypeError exception if
| its this value is not a Function object. Therefore, it cannot be
| transferred to other kinds of objects for use as a method.
Nevertheless, one might not expect that, whereas for IE6 and FF2 the
result starts with the characters f u n , in Opera 9.25 there is a
preceding LF character (number ten).

That behavior is ECMAScript-compliant and should not be unexpected after
reading the above section of the Specification.

Pedantic, but unreliable, pig-head.

I wrote ".toString() of a function is not defined.".
I did not write ".toString of a function is not defined.".

The existence of .toString is defined, and that .toString() returns a
string, but the string itself is not defined but only loosely indicated;
and that corresponds to the difference between what I wrote and what I
did not write.

Any normal person would have understood my meaning sufficiently quickly.
You do not understand English as well as you think you do.

There is nothing in the standard to make one EXPECT that a non-printing
character would appear at the beginning of the string. Variation from
the norm is compliant, but that does not imply that one should expect
such a form. Therefore, "one might not expect" is a sound statement of
the situation.

Why, however, do you quote an inferior standard? You should be reading
the more reliable ISO/IEC 16262.

KETFOB.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted:
Dr said:
Warning : .toString() of a function is not defined.
Yes, it is:

,-[ECMAScript Ed. 3 Final]
|
| 15.3.4.2 Function.prototype.toString ( )
|
| An implementation-dependent representation of the function is returned.
| This representation has the syntax of a FunctionDeclaration. Note in
| particular that the use and placement of white space, line terminators,
| and semicolons within the representation string is
| implementation-dependent.
| [...]
Nevertheless, one might not expect that, whereas for IE6 and FF2 the
result starts with the characters f u n , in Opera 9.25 there is a
preceding LF character (number ten).
That behavior is ECMAScript-compliant and should not be unexpected after
reading the above section of the Specification.

Pedantic, but unreliable, pig-head.

If you ever could post without insulting anyone?
I wrote ".toString() of a function is not defined.".

Which is wrong.
I did not write ".toString of a function is not defined.".

Which I did _not_ understood you said.
The existence of .toString is defined, and that .toString() returns a
string, but the string itself is not defined but only loosely indicated;

No, it is not. If you had cared to read thoroughly, it says the
representation is implementation-dependent *but* it has (read: has to be in)
the syntax of a FunctionDeclaration.
[...]
There is nothing in the standard to make one EXPECT that a non-printing
character would appear at the beginning of the string.

Yes, there is. If you had cared to read thoroughly, you would have observed
the following part of the quote:

Opera provides its own ECMAScript implementation, and LF *is* a white space
character. The implementation-dependent use of that character includes the
possibility of it occuring as first character of the returned representation
string which should therefore not be unexpected. Whether or not that
character would be non-printing or not is irrelevant.

So much for understanding English.
Why, however, do you quote an inferior standard? You should be reading
the more reliable ISO/IEC 16262.

You of all people should know that ISO/IEC 16262:2002 says exactly the same
as ECMA-262 Ed. 3 Final (2000-03). And both are *international* standards,
so none is inferior to the other in any way; the only difference between
them is the publishing standardization body.

Please remind me not to make an attempt to help you anymore in case you have
a problem or a question, such as in <[email protected]>. It is
just not worth the effort :-(


PointedEars
 

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
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top