View Full Version : Fatal Error with loops?
Guardian23
09-16-2002, 12:18 PM
I have a loop, well, a loop within a loop, that looks like this:
function findpos (top, here) {
tp = document.getElementById(top)
for (i=1; i<tp.firstChild.childNodes.length-1; i++) {
for (m=1; m<tp.firstChild.childNodes[i].childNodes.length-1; m++) {
if (here == tp.firstChild.childNodes[i].childNodes[m]) {
//if the here object and the object
//looped over are one and the same
place (i, m, curstem)//then call this function
//alert (i)
//alert (m)
//alert (curstem)
}
}
}
}
Which links to the function:
/******place() function******/
function place(ayc, axc, num) {
pos = document.getElementById("MI")
pos = pos.firstChild
for (i=0; i<stems[num].length; i++) {
x = stems[num][i][0] + axc;
y = stems[num][i][1] + ayc;
tog (pos.childNodes[y].childNodes[x])
}
}
This works fine, until I click the next to last row (forgot to mention:
this is linked to the onclick event handler of td's in 6 out of 8 rows
in an 8X8 table (-top -bottom)).
It generates a problem in IE6, causing it to go into a non-
respondant state (think coma), sort of like using a sleep command from
Perl in the head of the page, but more permanent.
And I was wondering if it's a problem with the loop,
or if it's something that's not in the code?
Guardian
beetle
09-16-2002, 04:43 PM
Can I see the whole page (link or post) I need to see the table structure, and what arguments are being passed to findpos()
It does sound like a loop problem, but not sure till I mess w/it.
Guardian23
09-17-2002, 03:50 AM
Here goes, it's all in there, including the non important file DOMCaller.js,
which is merely a thing I put in there to make sure of the existance
of all the appended nodes.
Guardian
Enclosing Zip file
PS. If I put the link pentonimos.js in the head, it doesn't load
the function early enough for reset() to be called. Are the links
in the head tag loaded after the page?
PSS. I stumbled across the problem with the loop mid-session,
so it's not actually in the debugging state yet, it's purely experimental
to see if I can mimic a (x,y) cooridinate plane successfully.
Guardian23
09-17-2002, 03:58 AM
???
huh?
Without looking at the rest of the code I can see one problem straigth away. The variable i is not being delclared local to the functions so it will be created global. The loop in the function place() will then mess up the loop in the function findpos().
Try changing the loops
for (var i=1; i<tp.firstChild.childNodes.length-1; i++)
for (var i=0; i<stems[num].length; i++)
Guardian23
09-18-2002, 01:30 AM
It seems to make sense, but I have a feeling it's not the actual
problem, although it would have side effects......
Let's see......
Guardian
*checks out*
.........
*and cashes in*
PS. It worked. But why did it crash my system before that?
(When it didn't work)
glenngv
09-18-2002, 03:36 AM
function findpos (top, here) {
tp = document.getElementById(top)
for (i=1; i<tp.firstChild.childNodes.length-1; i++) {
for (m=1; m<tp.firstChild.childNodes.childNodes.length-1; m++) {
if (here == tp.firstChild.childNodes.childNodes[m]) {
alert(i); [i]//i = the i in the outermost loop
place (i, m, curstem)//then call this function
alert (i) [i]//i = the i after the place() is called, which would be equal to stems[num].length
//this could make the i variable larger than the the condition in the outermost loop which is
//i<tp.firstChild.childNodes.length-1
//and it is still inside this innermost loop!
}
}
}
}
function place(ayc, axc, num) {
pos = document.getElementById("MI")
pos = pos.firstChild
for (i=0; i<stems[num].length; i++) {
x = stems[num][i][0] + axc;
y = stems[num][i][1] + ayc;
tog (pos.childNodes[y].childNodes[x])
}
}
hope this makes sense. :)
Guardian23
09-18-2002, 05:59 AM
So, the way I'm seeing it is like this:
#1 The outermost loop initiates the i variable.
#2 The if statement calls the function, passing i and m into it
#3 The innermost loop reinitiates the i variable
#4 The innermost loop returns the i variable (which is 5, being
the length of @stems)
#5 The loop-within-loop continues going around and around, be-
'cause 5 is smaller than the row number, which
is 6+1 (childNode[6]).
#6 The 5th row doesn't return an error, because 5 was the initial
value of i in the function call
So from my point of view, if I changed the innermost loop's
evaluation to stems.length-1, and incrementation to ++i it would
crash anywhere after the fourth+1 line (6,7)(counting is
different after meeting arrays:().
How about this solution though:
if (here == tp.firstChild.childNodes[i].childNodes[m]) {
place (i, m, curstem)//then call this function
break;//breaks out of loops, regardless of i,
//as long as the if statement evaluates as being true
}
Guardian
glenngv
09-18-2002, 06:13 AM
the best solution is to var your variables as RoyW said
as to your solution, it depends on the logic of your requirements, if you want to break out of the innermost loop once that condition is satisfied, then it's correct.
but it is always best to var your variables to avoid unexpected results. sometimes, it's very hard to debug when same "un-var" local variables are modified by several functions.
Guardian23
09-20-2002, 03:51 AM
Yeah, it's easier to implement it that way anyway.
Thanks for all the help guys! :)
Guardian
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.