Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using for loop to count through variables?

    I searched the forums and google and I kept ending up at irrelevant pages. Perhaps I wasn't searching for the correct term.

    Imagine I have a set of variables (Item1 - Item10) and I want to do something with their values. (anything really). How can I set up a FOR LOOP that will count from 1-10, add that as "string" to the end of "item" and then do my thing. Yes this would be easier with arrays, but it has others uses, I just need to know how to do it.

    The way I do it now (yikes)

    Code:
    Var item1 = "";
    Var item2 = "";
    Var item3 = "";
    (etc)

    How can I do it like this?

    Code:
    For (X=1; X <= 10; X++;) {
         Var item+x = "";
    }
    This is probably really easy.
    Thanks!

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For (X=1; X <= 10; X++ {
    window['item'+x] = "";
    }

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, that does trick!

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    More info on that trick here.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that link. It's nice to know why something works instead of just using it not knowing what it does. Much appreciated.


  •  

    Posting Permissions

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