Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: 1 + 1 will output 11 and not 2...

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 1 + 1 will output 11 and not 2...

    Hello, got a problem with addition, I have a function that will need a number. Like:
    my_function(numb)

    and now I want to get the next number (numb + 1) but It will output as 11 instead of 1 + 1 = 2..
    I tried to use this:
    var next = numb + 1..

    and this:
    var next = 0;
    next = numb + 1;

    and this:
    var one = 1;
    var next numb + one;

    but it will give me the same result all the time, the previous (-1) works great but not the +1..

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try numb++, usually that works.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks it worked.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The + operator in Javascript is overloaded: it performs two quite distinct functions. It performs arithmetical addition on numbers, but concatenation on strings. The latter always takes precedence over the first, so if a string is involved on either side of the operator, concatenation will occur. That is to say:
    Code:
    1 + 1 == 2;
    1 + "1" == "11";
    "1" + 1 == "11";
    If you're not sure whether you're dealing with a number or a string, be safe and convert it. There are several methods of doing this:
    Code:
    new Number(n);
    parseFloat(n, 10);
    parseInt(n, 10);
    n - 0;
    -n;
    +n;
    n * 1;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Isn't the easiest way, then to do
    a + 1*b; ?
    (Assuming you know that a is a constant, or that a is declared literally as an integer, say 3 + 1*b; )

    Or, you could just subtract it's opposite:

    a-(-b);

    That's probably the easiest, in fact. This would, since it's a mathematical operation without a string-alternative, convert both a and b, I'd assume, so it's a very efficient way to do all of this at once.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Are the (a+b*1) or (a- -b) shorthands executed faster than the parse functions?

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    My guess would be that any simple math function would be faster than any more complex function.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, +"3" performed an average of 0.0053ms faster than parseInt("3", 10) over 100,000 iterations in SpiderMonkey.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I would just do this:

    var next=numb-0+1;

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Personally, I prefer the form:
    Code:
    var next = +numb + 1;
    However, be aware that parse(Int|Float) don't do quite the same things as the simple operator trickery; they will ignore trailing characters, for a start. +"3j" is NaN, but parseInt("3j") is 3.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •