Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 12:27 PM
reynoldscraigr@hotmail.com
Guest
 
Posts: n/a
Default stuck in a loop

Hi All,

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

function RemoveMenuFromHoldArray(menuName) {
var i = 0;
for (i=0;i<=MenusToHoldOpen.length-1;i++) {
if (MenusToHoldOpen[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?
  #2  
Old July 20th, 2005, 12:28 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: stuck in a loop

<reynoldscraigr@hotmail.com> wrote in message
news:ea209b176d6a4a38995da77ad72f684b@news.teranew s.com...
<snip>[color=blue]
> function RemoveMenuFromHoldArray(menuName) {
> var i = 0;
> for (i=0;i<=MenusToHoldOpen.length-1;i++) {
> if (MenusToHoldOpen[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 (MenusToHoldOpen.length-1) or not, and as i gets bigger it
should eventually become bigger than (MenusToHoldOpen.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 <= (MenusToHoldOpen.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.


  #3  
Old July 20th, 2005, 12:29 PM
reynoldscraigr@hotmail.com
Guest
 
Posts: n/a
Default Re: stuck in a loop

On Thu, 30 Oct 2003 15:33:27 -0000, "Richard Cornford"
<Richard@litotes.demon.co.uk> wrote:
[color=blue]
><reynoldscraigr@hotmail.com> wrote in message
>news:ea209b176d6a4a38995da77ad72f684b@news.terane ws.com...
><snip>[color=green]
>> function RemoveMenuFromHoldArray(menuName) {
>> var i = 0;
>> for (i=0;i<=MenusToHoldOpen.length-1;i++) {
>> if (MenusToHoldOpen[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 (MenusToHoldOpen.length-1) or not, and as i gets bigger it
>should eventually become bigger than (MenusToHoldOpen.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 <= (MenusToHoldOpen.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).

  #4  
Old July 20th, 2005, 12:30 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: stuck in a loop

<reynoldscraigr@hotmail.com> wrote in message
news:eb2b2a62d268f60c95b0ff4f66d83a4d@news.teranew s.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.


  #5  
Old July 20th, 2005, 12:31 PM
reynoldscraigr@hotmail.com
Guest
 
Posts: n/a
Default Re: stuck in a loop

On Fri, 31 Oct 2003 19:25:20 -0000, "Richard Cornford"
<Richard@litotes.demon.co.uk> wrote:
[color=blue]
><reynoldscraigr@hotmail.com> wrote in message
>news:eb2b2a62d268f60c95b0ff4f66d83a4d@news.terane ws.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.

  #6  
Old July 20th, 2005, 12:31 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: stuck in a loop

<reynoldscraigr@hotmail.com> wrote in message
news:aeb57349bf9fd3f03e96beb2437583ab@news.teranew s.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.


  #7  
Old July 20th, 2005, 12:31 PM
Lee
Guest
 
Posts: n/a
Default 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.

  #8  
Old July 20th, 2005, 12:31 PM
reynoldscraigr@hotmail.com
Guest
 
Posts: n/a
Default Re: stuck in a loop

On 1 Nov 2003 09:41:52 -0800, Lee <REM0VElbspamtrap@cox.net> 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.
  #9  
Old July 20th, 2005, 12:31 PM
Lee
Guest
 
Posts: n/a
Default 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.

  #10  
Old July 20th, 2005, 12:31 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: stuck in a loop

<reynoldscraigr@hotmail.com> wrote in message
news:fe596de07418abc64632e74033a3921c@news.teranew s.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.


  #11  
Old July 20th, 2005, 12:32 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: stuck in a loop

"Richard Cornford" <richard@litotes.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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #12  
Old July 20th, 2005, 12:32 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: stuck in a loop

"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:oevurayz.fsf@hotpop.com...[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.


  #13  
Old July 20th, 2005, 12:32 PM
reynoldscraigr@hotmail.com
Guest
 
Posts: n/a
Default Re: stuck in a loop

On 2 Nov 2003 07:02:43 -0800, Lee <REM0VElbspamtrap@cox.net> 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles