Yes, recursive calls are definitely possible in JavaScript. I included a
quick little script to give an example of them in action.
In the code you can see that displayArray() calls itself until the all of
the child items have been returned of the parent item you call it with. In
the onLoad statement, we simply use displayArray("home value") and it will
display them all.
If we were to call it with displayArray("value one"), only the child
branches of that parent item would be returned.
****************** CODE ********************
<script>
var myArr = new Array();
function addToArr(parentID, thisValue){
var nextIndex = myArr.length;
myArr[nextIndex] = new Array();
myArr[nextIndex][0] = parentID;
myArr[nextIndex][1] = thisValue;
}
addToArr("home", "home value");
addToArr("home value", "value one");
addToArr("value one", "value one : one");
addToArr("value one : one", "value one : one : one");
addToArr("value one", "value one : two");
addToArr("home value", "value two");
addToArr("value two", "value two : one");
addToArr("value two", "value two : two");
function displayArray(parentID){
var displayValue = '';
for(var i = 0; i < myArr.length;i++){
if(myArr[i][0] == parentID){
displayValue = displayValue + myArr[i][1] + '\n';
displayValue = displayValue + displayArray(myArr[i][1]);
}
}
return displayValue;
}
</script>
<body onload="alert(displayArray('home value'));">
</body>
</html>
*****************CODE******************
Hope it helps,
Nate Nielsen
[EMAIL PROTECTED]
817.726.8644
Fuse your server and client syntax together !
www.FusionScript.com
----- Original Message -----
From: "craig girard" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, April 10, 2002 10:29 AM
Subject: OT: Javascript Recursion
> Anyone ever got recursion to work in JS? I am trying to output a tree
> structure and I can get JS to output a 'branch' but it does not return to
> node underneath and continue with those branches. when it gets to the end
> of a branch it just stops. Is true recursion possible in JS?
>
> does this make any sense? :)
>
> Thanks,
>
> Craig
>
>
==^================================================================
This email was sent to: [email protected]
EASY UNSUBSCRIBE click here: http://topica.com/u/?bUrFMa.bV0Kx9
Or send an email to: [EMAIL PROTECTED]
T O P I C A -- Register now to manage your mail!
http://www.topica.com/partner/tag02/register
==^================================================================