Loading ...
Sorry, an error occurred while loading the content.

skipping for loop parameters

Expand Messages
  • Joe Martin
    Iterating over an array backwards like: /*jslint plusplus: true */ var ii, foo = []; for (ii = foo.length; --ii;) { foo.push( hello ); } produces the error
    Message 1 of 4 , Jun 26, 2012
    • 0 Attachment
      Iterating over an array backwards like:

      /*jslint plusplus: true */

      var ii, foo = [];

      for (ii = foo.length; --ii;) {
      foo.push("hello");
      }

      produces the error "Expected an identifier and instead saw ')'." for
      the for line. Is there a reason that for parameters can't be skipped?

      The similar infinite loop case 'for (;;) {' works fine though.
    • Mike
      This to me is clearly a situation of code being too clever for its own good. Simply write it out the statement in full. What s so hard about that? ...
      Message 2 of 4 , Jun 26, 2012
      • 0 Attachment
        This to me is clearly a situation of code being too clever for its own good. Simply write it out the statement in full. What's so hard about that?

        Joe Martin <joe.martin@...> wrote:

        >Iterating over an array backwards like:
        >
        >/*jslint plusplus: true */
        >
        >var ii, foo = [];
        >
        >for (ii = foo.length; --ii;) {
        > foo.push("hello");
        >}
        >
        >produces the error "Expected an identifier and instead saw ')'." for
        >the for line. Is there a reason that for parameters can't be skipped?
        >
        >The similar infinite loop case 'for (;;) {' works fine though.


        [Non-text portions of this message have been removed]
      • Joe Martin
        ... What I wrote is a pretty common pattern but that s beside the point. I just want to know if for loops are supposed to be either all parameters or no
        Message 3 of 4 , Jun 26, 2012
        • 0 Attachment
          On Tue, Jun 26, 2012 at 7:24 PM, Mike <z_mikowski@...> wrote:

          > **
          >
          >
          > This to me is clearly a situation of code being too clever for its own
          > good. Simply write it out the statement in full. What's so hard about that?
          >
          >
          What I wrote is a pretty common pattern but that's beside the point. I just
          want to know if for loops are supposed to be either all parameters or no
          parameters. Skipping the initializer causes an error too (which causes an
          issue if you need to initialize out of the loop.)

          The errors kind of look accidental like maybe the parser is getting
          confused.


          [Non-text portions of this message have been removed]
        • douglascrockford
          ... Right. If all you need is a condition, use while. And don t get too tricky.
          Message 4 of 4 , Jun 26, 2012
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, Joe Martin <joe.martin@...> wrote:
            > I just
            > want to know if for loops are supposed to be either all parameters or no
            > parameters. Skipping the initializer causes an error too (which causes an
            > issue if you need to initialize out of the loop.)

            Right. If all you need is a condition, use while. And don't get too tricky.
          Your message has been successfully submitted and would be delivered to recipients shortly.