By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

stuck in a loop

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
<re************@hotmail.com> wrote in message
news:ea******************************@news.teranew s.com...
<snip>
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?


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.
Jul 20 '05 #2

P: n/a
On Thu, 30 Oct 2003 15:33:27 -0000, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> wrote:
<re************@hotmail.com> wrote in message
news:ea******************************@news.terane ws.com...
<snip>
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?


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.


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).

Jul 20 '05 #3

P: n/a
<re************@hotmail.com> wrote in message
news:eb******************************@news.teranew s.com...
<snip>
... never give a variable more scope than it
needs, ...
<snip>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.
yes.
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?
Yes.
Thus I'll make sure all my loop variables are declared
locally to avoid this problem (assuming I'm right).


Never give a variable more scope than it needs.

Richard.
Jul 20 '05 #4

P: n/a
On Fri, 31 Oct 2003 19:25:20 -0000, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> wrote:
<re************@hotmail.com> wrote in message
news:eb******************************@news.terane ws.com...
<snip>
... never give a variable more scope than it
needs, ...

<snip>
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.


yes.
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?


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


Never give a variable more scope than it needs.

Richard.


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.

Jul 20 '05 #5

P: n/a
<re************@hotmail.com> wrote in message
news:ae******************************@news.teranew s.com...
<snip>
... . 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.
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.)
I'm surpeised that it does it that way actually,
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.
as it could be very dangerous,
as I have found out the hard way.


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.
Jul 20 '05 #6

P: n/a
Lee
re************@hotmail.com said:
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.


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.

Jul 20 '05 #7

P: n/a
On 1 Nov 2003 09:41:52 -0800, Lee <RE**************@cox.net> wrote:
re************@hotmail.com said:
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.


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.


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.
Jul 20 '05 #8

P: n/a
Lee
re************@hotmail.com said:

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.


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.

Jul 20 '05 #9

P: n/a
<re************@hotmail.com> wrote in message
news:fe******************************@news.teranew s.com...
<snip>
... , 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.
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>Oh the things that you really miss from working with
a compiliable language, like syntax checking and scope
checking etc.


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

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

Richard.
Jul 20 '05 #10

P: n/a
"Richard Cornford" <ri*****@litotes.demon.co.uk> writes:
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.


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #11

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
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.
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.


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.
Jul 20 '05 #12

P: n/a
On 2 Nov 2003 07:02:43 -0800, Lee <RE**************@cox.net> wrote:
re************@hotmail.com said:

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.


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.


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
Jul 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.