I'm trying to get a specific converging series function worked out in JavaScript:
function cnvg(sum,marker){
if((marker--||1000)>=0){
return cnvg(sum=(sum||0) + 1/Math.pow(-3,marker)/(2*marker+1), marker)
} else {
return sum;
}
}
I'm expecting cnvg()
to come back with the equivalent of Math.PI/Math.sqrt(12)
but I keep getting a "Maximum call stack size exceeded" error. I thought it may be the number of iterations, so I dropped the 1000
reference to 100
then 10
and finally to 1
, but I still seem to be receiving the error.
Theoretically, once marker
has counted down to 0 and executes the last loop it should stop and return the value of sum
, but this doesn't seem to be the case... Can anyone tell me what I'm doing wrong?
Thanks in advance.
marker-- || 1000
will evaluate to 1000
when marker
is 1
or 0
, resulting in an infinite recursion. Did you mean:
function cnvg(sum, marker) {
marker = (typeof(marker) === 'undefined') ? 1000 : marker--;
if (marker >= 0) {
return cnvg(sum=(sum||0) + 1/Math.pow(-3,marker)/(2*marker+1), marker)
} else {
return sum;
}
}
0 Comment
NO COMMENTS