SIMPLE NUMBER COMPARISON

Discussion in 'Javascript' started by Window Frog, Dec 28, 2007.

  1. Window Frog

    Window Frog Guest

    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>
     
    Window Frog, Dec 28, 2007
    #1
    1. Advertising

  2. 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


    Only two things are infinite, the universe and human stupidity,
    and I'm not sure about the former. - Albert Einstein
     
    My Pet Programmer, Dec 28, 2007
    #2
    1. Advertising

  3. Window Frog

    Window Frog Guest

    On Dec 28, 9:49 am, My Pet Programmer <>
    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
    > > 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
    >
    >
    > 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.
     
    Window Frog, Dec 28, 2007
    #3
  4. Window Frog said:
    > On Dec 28, 9:49 am, My Pet Programmer <>
    > 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
    >>> 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
    >>
    >>
    >> 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


    Only two things are infinite, the universe and human stupidity,
    and I'm not sure about the former. - Albert Einstein
     
    My Pet Programmer, Dec 28, 2007
    #4
  5. In comp.lang.javascript message <fl32eb$raq$>
    , Fri, 28 Dec 2007 09:49:41, My Pet Programmer
    <> 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).

    --
    (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.
     
    Dr J R Stockton, Dec 29, 2007
    #5
  6. Dr J R Stockton wrote:
    > 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
    --
    "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>
     
    Thomas 'PointedEars' Lahn, Dec 30, 2007
    #6
  7. In comp.lang.javascript message <>, Sun,
    30 Dec 2007 02:11:42, Thomas 'PointedEars' Lahn <>
    posted:
    >Dr J R Stockton wrote:
    >> 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.

    --
    (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)
     
    Dr J R Stockton, Dec 30, 2007
    #7
  8. Dr J R Stockton wrote:
    > [...] Thomas 'PointedEars' Lahn [...] posted:
    >> Dr J R Stockton wrote:
    >>> 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:

    >> | 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 <>. It is
    just not worth the effort :-(


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 31, 2007
    #8
  9. In comp.lang.javascript message <>, Mon,
    31 Dec 2007 03:44:06, Thomas 'PointedEars' Lahn <>
    posted:
    >
    >> 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.
    >


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

    YGCIB.

    --
    (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)
     
    Dr J R Stockton, Dec 31, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith
    Replies:
    14
    Views:
    714
    Bengt Richter
    Mar 29, 2005
  2. Chandu
    Replies:
    3
    Views:
    336
    Emmanuel Delahaye
    Sep 19, 2005
  3. 一首诗
    Replies:
    2
    Views:
    335
  4. Deepu
    Replies:
    1
    Views:
    244
    ccc31807
    Feb 7, 2011
  5. db
    Replies:
    7
    Views:
    263
Loading...

Share This Page