SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to increment without a ++ operator?

    I have a for loop and would like to increment a variable for (let's say) 2 instead of one (++).
    How can I do that?

    I have tried for instance x + 2 instead of x++ but when I try it in IE an error saying that a script is making IE to run slowly and then nothing happens.
    So how to do this?

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    x = x + 2;
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ++ is actually an operator.

    If you structured it correctly - perhaps you could
    make +2 an operator in the same way you would with
    c++.

    Haven't ever tried this though.

    Just a thought.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    You didn't even read my post :(

    I said that x+2 doesn't work!

    Is there anything else that can be used?

    Check this two almost identical pages. First with x++ and the second with x+2. You'll see the difference.

    test #1
    test #2

  5. #5
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what i said was a bit too far beyond what you're trying to achieve.

    Ignore it.

    For anyone else:

    Is the functionality offered enough to overload an
    operator in Javascript as in a fully OO language.
    Could we create it as a function to replace it - without the need for brackets?

    At the very least we could create +2(v) - but i'm sure
    that there's an oportunity hidden to create an overloaded
    method for ++ ... in the same way that the constructor
    class is actually available in JS - i believe.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And i meant c++ the language - not c the variable to the method autoincrement.

    And i DID read your post and give you a good answer.
    Thank you very much!

    I'm not annoyed - just a bit rubbed up that you assumed.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AGAIN....

    x=x+2;

    -NOT-

    x+2;

    so

    for (var x=0; x<lenVar; x=x+2)
    {
    ...code for loop...
    }
    Last edited by beetle; Jul 31, 2002 at 06:11.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okie --- so nobody wants to talk about my idea

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do! Since my OO knowledge is so-so, I'd love to see how this is accomplished. H00k me up Flawless
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol - i will take a look at it in a mo.
    I was hoping someone else might want to pick the idea
    up and run with it.

    HOWEVER - i have just done some fairly cool searching work in JS - i might post that first.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  11. #11
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And the post that i said i'd do first is here:
    http://www.sitepointforums.com/showt...threadid=70473

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  12. #12
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't look at the array thing, but JavaScript doesn't come with a way of overloading functions as C++ has.

    aDog

  13. #13
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript doesn't come with overloaded Array Hashes
    or complex sort functions either... what's your point?

    The topic was how to achieve it - not how to grumble about
    it not existing in a nice laid out format.

    I somewhat doubt there's going to be an interface to the
    methods directly - but the existance of a constructor
    should lead in the right direction - i'll get onto it
    shortly.

    Try not to be so negative Adog.

    Anything is possible.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  14. #14
    SitePoint Zealot
    Join Date
    Nov 2000
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    x+=2;
    x++;
    ++x;
    --x;
    x--;
    x-=2;

    for (x = 0; x < 1000; x+=2){}
    Last edited by lfabbric; Jul 31, 2002 at 12:15.

  15. #15
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes! The += operator! I was forgetting about it...always using that for strings...forgot it works with integers too! Bravo lfabbric!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  16. #16
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    my apology :)

    So it's obvious that I'm not very good at JavaScript (also my english is not very good ;>) so I apologize to Flawless_koder and beetle.

  17. #17
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't sweat it. No big deal.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  18. #18
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good Call Ifabric

    Sometimes something's very obvious and we just miss it for ages!

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  19. #19
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Flawless, I'm astounded that you didn't point out the += operator straight away! I would have thought that would be obvious to a man of your talents!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  20. #20
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah - i know

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •