Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / December 2007



Tip: Looking for answers? Try searching our database.

SIMPLE NUMBER COMPARISON

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Window Frog - 28 Dec 2007 14:41 GMT
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>
My Pet Programmer - 28 Dec 2007 14:49 GMT
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
[quoted text clipped - 3 lines]
> 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">
>
[quoted text clipped - 5 lines]
> 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!
   

Signature

Anthony Levensalor
anthony@mypetprogrammer.com

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

Window Frog - 28 Dec 2007 15:15 GMT
On Dec 28, 9:49 am, My Pet Programmer <anth...@mypetprogrammer.com>
wrote:
> 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
[quoted text clipped - 39 lines]
> 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.
My Pet Programmer - 28 Dec 2007 15:19 GMT
Window Frog said:
> On Dec 28, 9:49 am, My Pet Programmer <anth...@mypetprogrammer.com>
> wrote:
[quoted text clipped - 41 lines]
> 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 a.s. The first time I went through that I
damn near killed my cat, no worries.

:)

~A!

Signature

Anthony Levensalor
anthony@mypetprogrammer.com

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

Dr J R Stockton - 29 Dec 2007 23:14 GMT
In comp.lang.javascript message <fl32eb$raq$1@registered.motzarella.org>
, Fri, 28 Dec 2007 09:49:41, My Pet Programmer
<anthony@mypetprogrammer.com> posted:
>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).

Signature

(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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Thomas 'PointedEars' Lahn - 30 Dec 2007 01:11 GMT
> Warning : .toString() of a function is not defined.

Yes, it is:

,-[ECMAScript Ed. 3 Final]

| 15.3.4.2 Function.prototype.toString ( )
|
[quoted text clipped - 7 lines]
| 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
Signature

"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
 -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

Dr J R Stockton - 30 Dec 2007 20:05 GMT
In comp.lang.javascript message <4776F04E.2040105@PointedEars.de>, Sun,
30 Dec 2007 02:11:42, Thomas 'PointedEars' Lahn <PointedEars@web.de>
posted:
>> Warning : .toString() of a function is not defined.
>
[quoted text clipped - 20 lines]
>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.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Randy Webb - 31 Dec 2007 01:08 GMT
Dr J R Stockton said the following on 12/30/2007 3:05 PM:

<snip>

> KETFOB.

Did you learn that behavior from Thomas or did he learn it from you?

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Thomas 'PointedEars' Lahn - 31 Dec 2007 02:44 GMT
> [...] Thomas 'PointedEars' Lahn [...] posted:
>>> Warning : .toString() of a function is not defined.
[quoted text clipped - 18 lines]
>
> 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:

>> | Note in particular that the use and placement of white space, line
>> | terminators, and semicolons within the representation string is
>> | implementation-dependent.

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.

> KETFOB.

Please remind me not to make an attempt to help you anymore in case you have
a problem or a question, such as in <47784E23.105@PointedEars.de>.  It is
just not worth the effort :-(

PointedEars
Dr J R Stockton - 31 Dec 2007 19:38 GMT
In comp.lang.javascript message <47785776.7010404@PointedEars.de>, Mon,
31 Dec 2007 03:44:06, Thomas 'PointedEars' Lahn <PointedEars@web.de>
posted:

>> I wrote           ".toString() of a function is not defined.".
>
[quoted text clipped - 3 lines]
>
>Which I did _not_ understood you said.

Well, that could be because I did not say it; or it could be mere folly
on your part.

YGCIB.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Randy Webb - 31 Dec 2007 01:06 GMT
Dr J R Stockton said the following on 12/29/2007 6:14 PM:

<snip>

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

<URL: http://jibbering.com/faq/index.html#FAQ5_1>
<URL: http://jibbering.com/faq/index.html#FAQ5_2>

Specifically, last sentence of paragraph 1 of 5.1.
Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.