473,788 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Jul 20 '05 #1
12 4905
<re************ @hotmail.com> wrote in message
news:ea******** *************** *******@news.te ranews.com...
<snip>
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?


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.
Jul 20 '05 #2
On Thu, 30 Oct 2003 15:33:27 -0000, "Richard Cornford"
<Ri*****@litote s.demon.co.uk> wrote:
<re*********** *@hotmail.com> wrote in message
news:ea******* *************** ********@news.t eranews.com...
<snip>
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?


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.


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
<re************ @hotmail.com> wrote in message
news:eb******** *************** *******@news.te ranews.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
On Fri, 31 Oct 2003 19:25:20 -0000, "Richard Cornford"
<Ri*****@litote s.demon.co.uk> wrote:
<re*********** *@hotmail.com> wrote in message
news:eb******* *************** ********@news.t eranews.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
<re************ @hotmail.com> wrote in message
news:ae******** *************** *******@news.te ranews.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
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
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
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
<re************ @hotmail.com> wrote in message
news:fe******** *************** *******@news.te ranews.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4261
by: ron | last post by:
have been stuck on this for several days now. I am trying to create a reverse polish calculator and I'm stuck at an intermediate stage. This is what I know I have to do (just not sure how to do it to much of a newbie :( ) I'm trying to call method, isOp, in the int method, evaluate. While in INT evaluate method and in a while(hasNextToken) loop. Calling isOp on each token, nextToken. If it returns False, convert to (Integer) and push...
0
1085
by: GeorgeAtkins | last post by:
VS 2003 using VB.NET I'm writing a program that reads text entries from a one file and matches them against values in an XML document. However, the code seems to just sit in the loop and never get anywhere, yet no errors occur. I tried this hard-coding a title known to be in the XML file and it worked ok. It seems to "hang" when the title is not found, but that's just a guess. Here is the code: Imports System.Xml Imports System.Text...
0
1916
by: Mark Phillips | last post by:
Hello, I am having a problem in which a file can get stuck open when a thread that is attempting to write to it gets aborted (ThreadAbortedException occurs). The log file gets stuck open until the process is shut down (cannot delete file from Windows Explorer, for example). My test application stops execution with: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process...
1
2697
by: Rene | last post by:
I got stuck on an infinite loop on the "Paint" event of my custom control and I was not able to "ctrl-break" my way out of it. After trying everything, I had to ctrs-alt-delete to shut down the application and delete the bin and obj folders of the control so that the application could start without locking. I don't understand? I am pretty sure I was able to press ctrl-break before and it would stop on the offending line. Could this be...
5
2783
by: Mike D | last post by:
Attached is my code. Which I know there is a better way of writing however this is what I came up with and it works until the value is null or not = to the <> value. 'Do While rdrSQL.Read() 'While rdrSQL.Read() ' Dim result As String ' result = Trim(rdrSQL.GetString(0))
7
7121
by: steve marchant | last post by:
trying to learn VB6. Simple counting loop which counts to 8 in 1 sec intervals, then starts from 1 again and repeats. Have two Command buttons on the form. Cmd1 starts the counting, and I need to know how to stop it with Cmd2. Here's my code so far: Private Sub Command1_Click() Dim x, y, m m = 1 Do Print m
11
1806
by: Tom C | last post by:
We have a window named FormApplication which just happens to be our mid parent window. When I open it in the designer, it is stuck in a loop redisplaying a c1flexgrid. maxing out our cpu's. By attaching to the process from another visual studio, I can get it to break on some user code with this huge callstack, most of which is native. So are we instantiating something in design mode that we should not be? Does this mean anything to anyone?...
2
1676
by: hexusnexus | last post by:
I wrote a simple algorithm and it keeps getting stuck in a loop. I guess I'm just to tired to figure it out: compcount= suitrank= trump=2 l,lt=0,0 while l<4: while lt<4:
1
1647
by: Cainnech | last post by:
Hi guys, I'm a bit stuck in an infinite loop. What I'm trying to do in the code below is to compare two arrays and if there's an item in array ALGEMEEN that is not in array ITEMS, I want to add it to ITEMS. Now the ITEMS-array looks like this: items=;
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.