recursively calling a function with an array[n] as the parameter

C

csx

Hi all,
Im trying to figure out how to recursively call a function in javascript. I
need to pass the array in the function, and the array parameter changes on
each recursive call to the function. The recursive function here is called
'count'.
The funciton is called like:
var cols = count(values[0]); // Here the recursive function is called
So I pass in the array 'values', element 0.

the array is defined like this
values[0] = new values(1, 1,'A',0);
values[1] = new values(2, 2,'B',1);
values[2] = new values(3, 3,'C',2);
values[3] = new values(4, 3,'D',2);
values[4] = new values(5, 3,'E',2);

upon each recursive call to the function, values[n] changes. How would this
be coded in javascript?
I suppose I could make the 'values' array global, then I would just have to
recursive pass in the array index. But this seems bad coding practice.
Any suggestions would be appreciated
- Thanks in advance!


<html>
<head>

<script language="javascript">
function values(key, row, desc, parent)
{
this.key = key;
this.row = row;
this.desc = desc;
this.parent = parent
}
</script>



<script language="javascript">

function count(values_array)
{
var sum = 0;

for (n=0; n<values.length; n++)
{
if (values_array.key == values[n].parent)
{
sum += countLeafNodes2(values[n]); // Need to recursive call this
function passing the values[n]
}
}
if (sum == 0)
return 1;
else
return sum;
*/
return 5;

}
</script>

</head>
<body>


</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top