473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

'for' loop being killed before it's finished.

bugboy
160 100+
Hi I'm trying to build tree type lists where each row in the list has several cells (columns). My code will render the first list ok then stops. The first 'for' statement should be moving to build the second list, but the loop is killed. The 'list', 'row' and 'cell' constructors are not shown.

Any idea what's killing the initial iteration.. is it the recursion in getChildren()?

Thanks for you help!

Expand|Select|Wrap|Line Numbers
  1. function  getlist()
  2.     { 
  3.     this.list = arguments[0].split(",");
  4.     this.render = function()
  5.         {
  6.         document.getElementById('workspace').innerHTML="";
  7.         for(x in this.list)
  8.             { 
  9.             var listid = this.list[x];
  10.             var rootid = list[listid].root;
  11.             document.getElementById('workspace').innerHTML += list[listid].html;
  12.             list[listid].resize();
  13.             resize('workspace', 'list.'+listid);
  14.             document.getElementById('list.'+listid).innerHTML += row[rootid].html;//render the root's html
  15.             getcells(rootid, 'row.'+rootid);
  16.             getChildren(rootid);
  17.             }
  18.         }
  19.     }
  20.  
  21. function getChildren(rowid)
  22.     {
  23.     for(n in row[rowid].children)
  24.         { 
  25.         var child = row[rowid].children[n];
  26.         document.getElementById('children.'+rowid).innerHTML += row[child].html;//add the child row html the the child container
  27.         getcells(child, 'row.'+child);
  28.         getChildren(child);//then get the child row's children
  29.         }
  30.     } 
  31.  
  32. function getcells(rowid, target)
  33.      {
  34.     for(y in row[rowid].cell)
  35.         { 
  36.         var cellid = row[rowid].cell[y];
  37.         document.getElementById(target).innerHTML += cell[cellid].html;//then add the cells to that child row html
  38.         }
  39.     }
Mar 29 '10 #1

✓ answered by bugboy

I've replaced the getChild for ...in loop with a while and it appears to work!

What was the for ..in loop doing wrong?

Expand|Select|Wrap|Line Numbers
  1. function getChildren(rowid)
  2.     {
  3.     var x = 0;
  4.     while(row[rowid].children[x])
  5.         { 
  6.         var child = row[rowid].children[x];
  7.         document.getElementById('children.'+rowid).innerHTML += row[child].html;//in the rows children container add the child row html
  8.         getcells(child, 'row.'+child);
  9.         getChildren(child);//then get the child row's children
  10.         x=x+1;
  11.         }
}

10 1920
RamananKalirajan
608 512MB
Hi bugboy,
Use try and catch block for the functions. Then you will know the source of the bug. Or you have to use alert and chek. Moreover in getChildren() you havent used return, since the function is a recursive() it will be expecting a return.

sample Code:

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.    //your code goes here.
  4. }
  5. catch(e)
  6. {
  7.  alert(e.message);
  8. }
Thanks and Regards
Ramanan Kalirajan
Mar 29 '10 #2
phvfl
173 Expert 100+
Is the list variable on line 10 a global? I can't see it declared anywhere.
Mar 29 '10 #3
bugboy
160 100+
'list' 'row' and 'cell' are all objects, i haven't shown their constructors.

'getChildren' and 'getCells' both write to the target element in the document so a return isn't needed as far as i know.
Mar 29 '10 #4
bugboy
160 100+
If i remove the call to it's self inside getChildren it works fine rendering both lists - except it won't get the children of the children so the tree/list only renders two levels deep. If i leave it in place the first tree/list renders fine with multiple levels but then the second list doesn't render at all, it's as if the initial 'for' loop in getList is just forgotten once recursion starts.
Mar 29 '10 #5
RamananKalirajan
608 512MB
When are you stopping the recursive function?

Thanks and Regards
Ramanan Kalirajan
Mar 29 '10 #6
Dormilich
8,658 Expert Mod 8TB
note: be cautious when using a for … in loop, it does not only fetch the numeric members.

what’s this? (line #26): row[child].html
Mar 29 '10 #7
bugboy
160 100+
The recursive function is called inside the for loop so it is only called once for each subchild, when there aren't any subchildren the for loop stops.

Sorry i'm not sure what you mean by not only numeric members.?

(line #26): row[child].html is just an html string being retrieved from the row object.
Mar 29 '10 #8
bugboy
160 100+
I've replaced the getChild for ...in loop with a while and it appears to work!

What was the for ..in loop doing wrong?

Expand|Select|Wrap|Line Numbers
  1. function getChildren(rowid)
  2.     {
  3.     var x = 0;
  4.     while(row[rowid].children[x])
  5.         { 
  6.         var child = row[rowid].children[x];
  7.         document.getElementById('children.'+rowid).innerHTML += row[child].html;//in the rows children container add the child row html
  8.         getcells(child, 'row.'+child);
  9.         getChildren(child);//then get the child row's children
  10.         x=x+1;
  11.         }
}
Mar 29 '10 #9
Dormilich
8,658 Expert Mod 8TB
@bugboy
numeric members: list[0], list[1], etc. for … in also loops over custom members (anything that is not a numbered member)
Mar 29 '10 #10
bugboy
160 100+
Ahh right, Thanks for your help guys!

I'm not sure what the problem was, my arrays only have successive numeric members.. hard to pick the best answer..
Mar 29 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
39
by: jabailo | last post by:
I am looping through a text file, and with each row, I launch a web service, asynchronously. Before I move on to the next step in the process, I want to make sure that all the web services have...
3
by: Richard Cavell | last post by:
I have a recursive function like this: void MyFunction(int i) { i++; // do something with i if (we're not finished) MyFunction(i); } I keep count of how far nested we are with i. Now, it...
6
by: F-13 | last post by:
I'm working on a BOM in Access 200 from an example downloaded from from the web. The sample database contains three tables, Assemblies (the list of items needed to assemble any assembly),...
11
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then...
5
by: eb65 | last post by:
I have a need to write a Windows Service application in VB.Net that needs to continuously do some processing, wait ten minutes, then repeat. What is a good approach to coding this type of thing?...
5
by: mturner64 | last post by:
Good day. I am working on a program that will allow a user to enter an undefined series of numbers and enter -99 when finished. The program should output the Greatest and Least of the series...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
9
by: saravanan82 | last post by:
Hi All, I tried to execute follwoing program, which got killed by the OS. #include<stdlib.h> main() { while(1) { char *c=(char*)malloc(10000); } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.