stuck in a loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • reynoldscraigr@hotmail.com

    stuck in a loop

    Hi All,

    hope someone can see what wrong here I have the following function

    function RemoveMenuFromH oldArray(menuNa me) {
    var i = 0;
    for (i=0;i<=MenusTo HoldOpen.length-1;i++) {
    if (MenusToHoldOpe n[i] == menuName) {
    MenusToHoldOpen .splice(i,1);
    return;
    }
    }
    }


    I have found through trial and error that if I do not have the "var i
    = 0;" line in the code, the function gets stuck in a loop.

    can anyone tell me why , or where the logic is wrong?
  • Richard Cornford

    #2
    Re: stuck in a loop

    <reynoldscraigr @hotmail.com> wrote in message
    news:ea209b176d 6a4a38995da77ad [email protected] ranews.com...
    <snip>[color=blue]
    > function RemoveMenuFromH oldArray(menuNa me) {
    > var i = 0;
    > for (i=0;i<=MenusTo HoldOpen.length-1;i++) {
    > if (MenusToHoldOpe n[i] == menuName) {
    > MenusToHoldOpen .splice(i,1);
    > return;
    > }
    > }
    > }
    >
    >
    >I have found through trial and error that if I do not have
    >the "var i = 0;" line in the code, the function gets stuck in a loop.
    >
    > can anyone tell me why , or where the logic is wrong?[/color]

    The difference between including var i = 0; and omitting it is that with
    the - var - declaration i becomes a (function) local variable and
    without it i is global. Good programming practice says never give a
    variable more scope than it needs, so in this case i should be local.

    But for the function above that should make no difference to the way the
    function operates as the only code that alters i is the initialisation
    to zero and the post increment operator. So i should always be less than
    or equal to (MenusToHoldOpe n.length-1) or not, and as i gets bigger it
    should eventually become bigger than (MenusToHoldOpe n.length-1) and the
    loop should terminate.

    The inappropriate use of global variables as loop counters often becomes
    a problem if the body of a loop calls another function that is itself
    using a global variable with the same identifier. But in the code above
    the only function call (and therefor unseen code) is the
    MenusToHoldOpen .splice(i, 1) call, and, assuming that MenusToHoldOpen is
    an Array and splice is Array.prototype .splice, splice should be a
    native-code function and not interact with any global variables.

    Incidentally, (i <= (MenusToHoldOpe n.length-1)) should be the same as (i
    < MenusToHoldOpen .length) but not require the subtraction on each test.
    Also I would use the - break; - statement to terminate the - for - loop
    instead of - return.

    Richard.


    Comment

    • reynoldscraigr@hotmail.com

      #3
      Re: stuck in a loop

      On Thu, 30 Oct 2003 15:33:27 -0000, "Richard Cornford"
      <Richard@litote s.demon.co.uk> wrote:
      [color=blue]
      ><reynoldscraig [email protected]> wrote in message
      >news:ea209b176 d6a4a38995da77a [email protected] eranews.com...
      ><snip>[color=green]
      >> function RemoveMenuFromH oldArray(menuNa me) {
      >> var i = 0;
      >> for (i=0;i<=MenusTo HoldOpen.length-1;i++) {
      >> if (MenusToHoldOpe n[i] == menuName) {
      >> MenusToHoldOpen .splice(i,1);
      >> return;
      >> }
      >> }
      >> }
      >>
      >>
      >>I have found through trial and error that if I do not have
      >>the "var i = 0;" line in the code, the function gets stuck in a loop.
      >>
      >> can anyone tell me why , or where the logic is wrong?[/color]
      >
      >The difference between including var i = 0; and omitting it is that with
      >the - var - declaration i becomes a (function) local variable and
      >without it i is global. Good programming practice says never give a
      >variable more scope than it needs, so in this case i should be local.
      >
      >But for the function above that should make no difference to the way the
      >function operates as the only code that alters i is the initialisation
      >to zero and the post increment operator. So i should always be less than
      >or equal to (MenusToHoldOpe n.length-1) or not, and as i gets bigger it
      >should eventually become bigger than (MenusToHoldOpe n.length-1) and the
      >loop should terminate.
      >
      >The inappropriate use of global variables as loop counters often becomes
      >a problem if the body of a loop calls another function that is itself
      >using a global variable with the same identifier. But in the code above
      >the only function call (and therefor unseen code) is the
      >MenusToHoldOpe n.splice(i, 1) call, and, assuming that MenusToHoldOpen is
      >an Array and splice is Array.prototype .splice, splice should be a
      >native-code function and not interact with any global variables.
      >
      >Incidentally , (i <= (MenusToHoldOpe n.length-1)) should be the same as (i
      >< MenusToHoldOpen .length) but not require the subtraction on each test.
      >Also I would use the - break; - statement to terminate the - for - loop
      >instead of - return.
      >
      >Richard.
      >[/color]

      Right.

      So if I'm understanding this correctly, those function I have that
      contain a loop based on a variable called "i", will all reference the
      same variable if it is not declared locally to each function.

      Which would naturally enough cause clashes on value of that variable,
      especially if a function with a loop calls a nother function with a
      loop.

      Correct?


      Thus I'll make sure all my loop variables are declared locally to
      avoid this problem (assuming I'm right).

      Comment

      • Richard Cornford

        #4
        Re: stuck in a loop

        <reynoldscraigr @hotmail.com> wrote in message
        news:eb2b2a62d2 68f60c95b0ff4f6 [email protected] ranews.com...
        <snip>[color=blue][color=green]
        >>... never give a variable more scope than it
        >>needs, ...[/color][/color]
        <snip>[color=blue]
        >So if I'm understanding this correctly, those function I have
        >that contain a loop based on a variable called "i", will all
        >reference the same variable if it is not declared locally to
        >each function.[/color]

        yes.
        [color=blue]
        >Which would naturally enough cause clashes on value of that
        >variable, especially if a function with a loop calls another
        >function with a loop.
        >
        >Correct?[/color]

        Yes.
        [color=blue]
        >Thus I'll make sure all my loop variables are declared
        >locally to avoid this problem (assuming I'm right).[/color]

        Never give a variable more scope than it needs.

        Richard.


        Comment

        • reynoldscraigr@hotmail.com

          #5
          Re: stuck in a loop

          On Fri, 31 Oct 2003 19:25:20 -0000, "Richard Cornford"
          <Richard@litote s.demon.co.uk> wrote:
          [color=blue]
          ><reynoldscraig [email protected]> wrote in message
          >news:eb2b2a62d 268f60c95b0ff4f [email protected] eranews.com...
          ><snip>[color=green][color=darkred]
          >>>... never give a variable more scope than it
          >>>needs, ...[/color][/color]
          ><snip>[color=green]
          >>So if I'm understanding this correctly, those function I have
          >>that contain a loop based on a variable called "i", will all
          >>reference the same variable if it is not declared locally to
          >>each function.[/color]
          >
          >yes.
          >[color=green]
          >>Which would naturally enough cause clashes on value of that
          >>variable, especially if a function with a loop calls another
          >>function with a loop.
          >>
          >>Correct?[/color]
          >
          >Yes.
          >[color=green]
          >>Thus I'll make sure all my loop variables are declared
          >>locally to avoid this problem (assuming I'm right).[/color]
          >
          >Never give a variable more scope than it needs.
          >
          >Richard.
          >[/color]

          cool, glad I got that sorted. For some reason I thought that I
          remembered javascript creating the required variable for the loop, and
          keeping it local, but maybe I was thinking of something else.

          I'm surpeised that it does it that way actually, as it could be very
          dangerous, as I have found out the hard way.

          Comment

          • Richard Cornford

            #6
            Re: stuck in a loop

            <reynoldscraigr @hotmail.com> wrote in message
            news:aeb57349bf 9fd3f03e96beb24 [email protected] ranews.com...
            <snip>[color=blue]
            >... . For some reason I thought that I remembered
            >javascript creating the required variable for the loop, and
            >keeping it local, but maybe I was thinking of something else.[/color]

            A normal formulation of a - for - statement would be along the lines
            of:-

            for(var c = 0;c < x;c++){
            ...
            }

            Placing the - var - in the - for - statement. But because JavaScript
            identifies - var - when it initially reads the code, and creates a local
            variable for each - var - it has found within a function body as the
            program enters the execution context of a call to that function, placing
            the - var - in the first expression of the - for - statement has exactly
            the same effect of using - var - to declare a local variable at the
            start (or even at the end) of a function body. Habitually declaring the
            counter of a - for - loop with - var - in the first expression just
            makes it difficult for the counter to accidentally be global.
            (JavaScript does not have a problem if you declare the same identifier
            as a local variable repeatedly, it only creates on variable for each
            name used.)
            [color=blue]
            >I'm surpeised that it does it that way actually,[/color]

            What would be the alternative? The interpreter couldn't second-guess the
            intention of the programmer. Even an effective AI would struggle to get
            that right all of the time and otherwise the decisions would have to be
            rule based, as they are now.
            [color=blue]
            >as it could be very dangerous,
            >as I have found out the hard way.[/color]

            You can shoot yourself in the foot in any programming language.
            Understanding what you are doing is the best defence, and for
            understanding the behaviour of JavaScript the language specification
            (ECMA 262) is the best guide.

            Richard.


            Comment

            • Lee

              #7
              Re: stuck in a loop

              reynoldscraigr@ hotmail.com said:
              [color=blue]
              >cool, glad I got that sorted. For some reason I thought that I
              >remembered javascript creating the required variable for the loop, and
              >keeping it local, but maybe I was thinking of something else.
              >
              >I'm surpeised that it does it that way actually, as it could be very
              >dangerous, as I have found out the hard way.[/color]

              It sounds like you're thinking of loop variables as being
              somehow different from other variables. That's not true.

              All variables are subject to being modified by function
              calls unless they're declared locally, so all variables
              should be declared where they are used.

              Comment

              • reynoldscraigr@hotmail.com

                #8
                Re: stuck in a loop

                On 1 Nov 2003 09:41:52 -0800, Lee <REM0VElbspamtr [email protected]> wrote:
                [color=blue]
                >reynoldscraigr @hotmail.com said:
                >[color=green]
                >>cool, glad I got that sorted. For some reason I thought that I
                >>remembered javascript creating the required variable for the loop, and
                >>keeping it local, but maybe I was thinking of something else.
                >>
                >>I'm surpeised that it does it that way actually, as it could be very
                >>dangerous, as I have found out the hard way.[/color]
                >
                >It sounds like you're thinking of loop variables as being
                >somehow different from other variables. That's not true.
                >
                >All variables are subject to being modified by function
                >calls unless they're declared locally, so all variables
                >should be declared where they are used.[/color]

                I wasn't thinking of them being special as such, just I thought that
                if it wasn't an already declared variable then it would be created
                locally, not globally. That obvioulsy isn't the case.

                I may have gotten confused with another loagnauge I was looking at a
                while back, but If I was I can't remember which language.

                Oh the things that you really miss from working with a compiliable
                language, like syntax checking and scope checking etc.

                Comment

                • Lee

                  #9
                  Re: stuck in a loop

                  reynoldscraigr@ hotmail.com said:
                  [color=blue]
                  >
                  >I wasn't thinking of them being special as such, just I thought that
                  >if it wasn't an already declared variable then it would be created
                  >locally, not globally. That obvioulsy isn't the case.
                  >
                  >I may have gotten confused with another loagnauge I was looking at a
                  >while back, but If I was I can't remember which language.
                  >
                  >Oh the things that you really miss from working with a compiliable
                  >language, like syntax checking and scope checking etc.[/color]

                  Most compiled languages require you to declare all variables.
                  The fact that Javascript *doesn't* is what got you into
                  trouble.
                  Scope and syntax checking aren't much use if a typo can
                  result in the creation of a new variable.

                  Comment

                  • Richard Cornford

                    #10
                    Re: stuck in a loop

                    <reynoldscraigr @hotmail.com> wrote in message
                    news:fe596de074 18abc64632e7403 [email protected] ranews.com...
                    <snip>[color=blue]
                    >... , just I
                    >thought that if it wasn't an already declared variable
                    >then it would be created locally, not globally. That
                    >obvioulsy isn't the case.[/color]

                    Yes, I suppose the decision could have gone either way. It probably
                    seemed easier, having traversed the scope chain to the global object in
                    order to determine whether the identifier could be resolved to just add
                    it as a property to that object rather than going back to the variable
                    object in the execution context.

                    <snip>[color=blue]
                    >Oh the things that you really miss from working with
                    >a compiliable language, like syntax checking and scope
                    >checking etc.[/color]

                    You might find Douglas Crokford's JSLINT program useful in that
                    context:-

                    <URL: http://www.crockford.com/javascript/jslint.html >

                    Richard.


                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: stuck in a loop

                      "Richard Cornford" <richard@litote s.demon.co.uk> writes:
                      [color=blue]
                      > Yes, I suppose the decision could have gone either way. It probably
                      > seemed easier, having traversed the scope chain to the global object in
                      > order to determine whether the identifier could be resolved to just add
                      > it as a property to that object rather than going back to the variable
                      > object in the execution context.[/color]

                      I doubt that is the reason. More likely it was to make it easier for
                      people who don't know the first thing about scopes, to write Javascript.
                      Making variables global removed the need for explaining scope.

                      /L
                      --
                      Lasse Reichstein Nielsen - [email protected]
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Richard Cornford

                        #12
                        Re: stuck in a loop

                        "Lasse Reichstein Nielsen" <[email protected] > wrote in message
                        news:oevurayz.f [email protected].. .[color=blue][color=green]
                        >>Yes, I suppose the decision could have gone either way. It
                        >>probably seemed easier, having traversed the scope chain to
                        >>the global object in order to determine whether the identifier
                        >>could be resolved to just add it as a property to that object
                        >>rather than going back to the variable object in the execution
                        >>context.[/color][/color]
                        [color=blue]
                        >I doubt that is the reason. More likely it was to make it
                        >easier for people who don't know the first thing about scopes,
                        >to write Javascript. Making variables global removed the need
                        >for explaining scope.[/color]

                        Yes, maybe. Though making undeclared variables become global doesn't
                        remove the need to explain scope, it just delays it and allows a lot of
                        bad habits to develop in the mean while.

                        Richard.


                        Comment

                        • reynoldscraigr@hotmail.com

                          #13
                          Re: stuck in a loop

                          On 2 Nov 2003 07:02:43 -0800, Lee <REM0VElbspamtr [email protected]> wrote:
                          [color=blue]
                          >reynoldscraigr @hotmail.com said:
                          >[color=green]
                          >>
                          >>I wasn't thinking of them being special as such, just I thought that
                          >>if it wasn't an already declared variable then it would be created
                          >>locally, not globally. That obvioulsy isn't the case.
                          >>
                          >>I may have gotten confused with another loagnauge I was looking at a
                          >>while back, but If I was I can't remember which language.
                          >>
                          >>Oh the things that you really miss from working with a compiliable
                          >>language, like syntax checking and scope checking etc.[/color]
                          >
                          >Most compiled languages require you to declare all variables.
                          >The fact that Javascript *doesn't* is what got you into
                          >trouble.
                          >Scope and syntax checking aren't much use if a typo can
                          >result in the creation of a new variable.[/color]

                          that what I meant. having to declare everything up front usually stops
                          things like this from happening. PLus you can check syntax etc,
                          without having to actually run the code

                          Comment

                          Working...