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

2874Re: [jslint] Nested loops with the same control vars

Expand Messages
  • Max
    May 9, 2012
      What might be the general rule be, just literally "Nested for loops
      shouldn't share control variables"? Or is there a larger principle at
      play here?

      On 5/9/2012 9:00 AM, Martin Cooper wrote:
      >
      > I was surprised to find that JSLint doesn't complain about the
      > following code:
      >
      > function doCheck(arr1, arr2, checker) {
      > var i, l;
      > for (i = 0, l = arr1.length; i < l; i = i + 1) {
      > for (i = 0, l = arr2.length; i < l; i = i + 1) {
      > checker(arr1[i], arr2[i]);
      > }
      > }
      > }
      >
      > I expected it to catch the fact that 'i' and 'l' are being used as
      > control vars in both loops, the inner one messing up the state of the
      > outer one.
      >
      > Seems like this would be a reasonable check for JSLint to have. I
      > can't think of a situation in which this code structure would be a
      > good thing.
      >
      > Thoughts?
      >
      > --
      > Martin Cooper
      >
      > __._,_.__
    • Show all 5 messages in this topic