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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts

    for loop question

    How do I re-write this from VBA to JAVASCRIPT?

    Code:
    For i = 0 To num_fields - 1
    Forgive me, im still new at this stuff.

    Thanks.

    Jay

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,641
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Code:
    for (i = 0; i < num_fields - 1; i++)
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    jason_kelly (01-25-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Code:
    for ( var i = 0; i < num_fields; ++i ) 
    {
        ....
    } // this closing brace is the equivalent of NEXT
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    jason_kelly (01-25-2012)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Quote Originally Posted by felgall View Post
    Code:
    for (i = 0; i < num_fields - 1; i++)
    No, not quite.

    The VBA loop ends on the value that is *EQUAL* to the expression after the TO. (Less than or equal, actually, but equal when the STEP value is 1, which is the default.)


    So you need to use i <= num_fields - 1

    Or, as I did, get rid of the -1 and use i < num_fields
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts
    Worked like a charm, still working on loop parts...I still have lots to learn.

    Thanks all!

    Cheers

    Jay

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts
    What about when theres a:

    Code:
    next i
    How would that be written in Javascript?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Read my first post. Again. Pay attention to where it says
    // this closing brace is the equivalent of NEXT
    If you mean, how do you specify Next i versus Next j, you don't. You will notice that the ability to specify a variable with Next has been removed from dialects of BASIC more modern than VBA. It never was a good idea.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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