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>
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>