addition

Discussion in 'Javascript' started by Paul, Dec 6, 2006.

  1. Paul

    Paul Guest

    Why is the addition here adding the number as a string but the subtraction
    works fine?

    function boxchange(box) {
    iv= dbform.ut.value
    if (box.checked == true) {
    iv = iv - box.value
    }
    else {
    iv = iv + box.value
    }
    dbform.ut.value=iv
    }

    so on the addition with the ut text box having a value of 1 It adds 1+2 and
    gets 12 ,but on the subtraction 12 -1 gives 11
     
    Paul, Dec 6, 2006
    #1
    1. Advertising

  2. Paul

    VK Guest

    Paul wrote:
    > Why is the addition here adding the number as a string but the subtraction
    > works fine?
    >
    > function boxchange(box) {
    > iv= dbform.ut.value
    > if (box.checked == true) {
    > iv = iv - box.value
    > }
    > else {
    > iv = iv + box.value
    > }
    > dbform.ut.value=iv
    > }
    >
    > so on the addition with the ut text box having a value of 1 It adds 1+2 and
    > gets 12 ,but on the subtraction 12 -1 gives 11


    Because form control value is string by default. As there is no
    substraction operator for strings, the engine tries to convert both
    strings into numbers.

    But addition operator acts as concatenator for string values, so
    stringValue + stringValue is a valid string concatenation statement, no
    conversion into numbers will be done.

    iv-= box.value;
    ....
    iv+= (+box.value);
     
    VK, Dec 6, 2006
    #2
    1. Advertising

  3. Paul

    Paul Guest

    Paul wrote:
    > Why is the addition here adding the number as a string but the subtraction
    > works fine?
    >
    > function boxchange(box) {
    > iv= dbform.ut.value
    > if (box.checked == true) {
    > iv = iv - box.value
    > }
    > else {
    > iv = iv + box.value
    > }
    > dbform.ut.value=iv
    > }
    >
    > so on the addition with the ut text box having a value of 1 It adds 1+2
    > and
    > gets 12 ,but on the subtraction 12 -1 gives 11


    Because form control value is string by default. As there is no
    substraction operator for strings, the engine tries to convert both
    strings into numbers.

    But addition operator acts as concatenator for string values, so
    stringValue + stringValue is a valid string concatenation statement, no
    conversion into numbers will be done.

    iv-= box.value;
    ....
    iv+= (+box.value);


    Thanks got it now
     
    Paul, Dec 6, 2006
    #3
  4. Paul

    Matt Kruse Guest

    Matt Kruse, Dec 6, 2006
    #4
  5. Paul

    ASM Guest

    Paul a écrit :
    > Why is the addition here adding the number as a string but the subtraction
    > works fine?


    value of a text field is always a string (not a number)

    > function boxchange(box) {
    > iv= dbform.ut.value
    > if (box.checked == true) {
    > iv = iv - box.value


    or iv -= box.value

    > }
    > else {
    > iv = iv + box.value


    iv = iv + Number(box.value);
    or
    iv = iv + box.value*1
    or
    iv = +box.value+iv;

    > }
    > dbform.ut.value=iv
    > }



    > so on the addition with the ut text box having a value of 1 It adds 1+2


    the sign + is used both to add text or numbers and JS doesn't know in
    advance if '2' is or isn't a character for you
    (here, as '2' comes from a text field
    it is a character without hesitation)

    > gets 12 ,but on the subtraction 12 -1 gives 11


    no mistake possible with '-' because it is only used with numbers


    --
    Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    Stephane Moriaux and his (less) old Mac already out of date
     
    ASM, Dec 6, 2006
    #5
  6. In comp.lang.javascript message
    <>, Wed, 6 Dec 2006 21:26:04,
    Paul <> wrote:
    >Why is the addition here adding the number as a string but the subtraction
    >works fine?


    >if (box.checked == true) {

    ^^^^^^^^ superfluous.

    It's a good idea to read the newsgroup and its old FAQ before posting.
    See below. That way you can save the time of everyone here, including
    yourself. And you'll discover other things too, including something
    about the formatting of newsgroup replies.

    FAQ 4.21 refers.

    Looking at your code :
    (1) IV is not declared, hence is global. Declare it local, with var.
    (2) Rather than evaluating box.value twice, it is neater to do so once
    and use it twice.

    var IV = dbform.ut.value
    var BV = box.value
    box.checked ? IV += BV : IV -= BV

    IV and BV are still strings, hence concatenation still occurs ...

    If a text control is for entry of a pure number, and no specific
    validation of the textual form will be done, then I recommend
    (a) Reading it once, into a variable
    (b) and getting a Number in the variable, by using unary + (or -).

    IV = +dbform.ut.value
    var BV = +box.value
    IV += box.checked ? +BV : -BV // addition/subtraction

    One of those + is now superfluous, but should be retained for
    readability.


    REGULARS :
    When someone answers a question that is manifestly treated in the FAQ,
    he/she is presumably ignorant of the FAQ and liable to have other
    questions treated in the FAQ. Therefore, your answers should include a
    reference to the FAQ.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    <URL:http://www.jibbering.com/faq/> Old RC FAQ of news:comp.lang.javascript
    <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 7, 2006
    #6
    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. Midnight Java Junkie

    Office Development (feature addition)

    Midnight Java Junkie, Jun 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    473
    Midnight Java Junkie
    Jun 20, 2004
  2. lezah
    Replies:
    7
    Views:
    795
    Mike Treseler
    Mar 2, 2004
  3. Julian

    Addition of one

    Julian, Oct 18, 2004, in forum: VHDL
    Replies:
    13
    Views:
    3,718
    rickman
    Oct 21, 2004
  4. Clemens Bosch

    Beginners questions for addition

    Clemens Bosch, Dec 2, 2004, in forum: VHDL
    Replies:
    0
    Views:
    578
    Clemens Bosch
    Dec 2, 2004
  5. jiten

    about addition operator

    jiten, Jun 22, 2005, in forum: VHDL
    Replies:
    3
    Views:
    4,522
Loading...

Share This Page