admin管理员组文章数量:1428500
Trying to get my fibonacci sequence to work using recursion but am running into the error maximum callstack exceeded
.
Code:
var genFib = function(count, limit, fibArray) {
if (count === undefined || count === null) {
var count = 0;
}
if (fibArray === undefined || fibArray === null) {
var fibArray = [0, 1];
}
if (count === limit) {
console.log(fibArray);
return fibArray;
}
var pushFibNo = function(fibArray) {
fibArray.push(fibArray[fibArray.length - 1] + fibArray[fibArray.length - 2]);
return fibArray;
};
// console.log(count++);
// console.log(limit);
// console.log(pushFibNo(fibArray));
return genFib(count++, limit, pushFibNo(fibArray));
};
genFib(null, 50, null);
The three console.logs
towards the bottom are logging out correct numbers, but I'm still getting the maximum callstack
error.
Trying to get my fibonacci sequence to work using recursion but am running into the error maximum callstack exceeded
.
Code:
var genFib = function(count, limit, fibArray) {
if (count === undefined || count === null) {
var count = 0;
}
if (fibArray === undefined || fibArray === null) {
var fibArray = [0, 1];
}
if (count === limit) {
console.log(fibArray);
return fibArray;
}
var pushFibNo = function(fibArray) {
fibArray.push(fibArray[fibArray.length - 1] + fibArray[fibArray.length - 2]);
return fibArray;
};
// console.log(count++);
// console.log(limit);
// console.log(pushFibNo(fibArray));
return genFib(count++, limit, pushFibNo(fibArray));
};
genFib(null, 50, null);
The three console.logs
towards the bottom are logging out correct numbers, but I'm still getting the maximum callstack
error.
- How many numbers (aprox) are you getting before getting the error? – Soren Commented Mar 15, 2016 at 16:14
-
Found it- you cannot pass in
count++
as a parameter in the return statement towards the bottom, you have to pass incount += 1
. Can anyone explain why? – user6017908 Commented Mar 15, 2016 at 16:17 - See my answer below, you are always using the same count. – pishpish Commented Mar 15, 2016 at 16:20
- 1 Both answers are correct – Kevin B Commented Mar 15, 2016 at 16:20
- I think count++ increments by 1 after you send it into the recursive call so you're actually always sending in the initial value, in this case zero. See @janje's answer – ThisClark Commented Mar 15, 2016 at 16:21
3 Answers
Reset to default 11The behaviour of ++
is different in postfix and prefix notation.
From MDN:
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.
If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.
This means that you are always passing count
before incrementing it, resulting in stack overflow.
To solve your problem, change
return genFib(count++, limit, pushFibNo(fibArray));
To
return genFib(++count, limit, pushFibNo(fibArray));
if (count === undefined || count === null) {
var count = 0;
}
you have declared "count" again. this overrides the count parameter and the if(count === limit) is never called.
The problem was that you was using the postincrement in this line
return genFib(count++, limit, pushFibNo(fibArray));
Then you always called the fucntion with the same value for "count", if you use the preoperator should works.
return genFib(++count, limit, pushFibNo(fibArray));
本文标签: JavaScript fibonacci using recursionStack Overflow
版权声明:本文标题:JavaScript fibonacci using recursion - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745529283a2661984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论