I try to make a function that returns an arrays of fib sequence up to Nth term. I’ve done this using iteration and returning n-th term alone with recursion but I can’t understand how to save the terms in the array using recursion.
I tried with this but it only returns last term and not as an array but a number
function fibRec(n) {
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
}
let array = [];
return (array[n] = fibRec(n - 1) + fibRec(n - 2));
}
console.log(fibRec(10))
3
Answers
You are not pushing to the array
Your
array
variable is created for each execution ofFib
, so you have multiple of them, and every one of them starts out empty, and then gets one assignment to itsn
index, and is then discarded (since it is local).Moreover, if you want the caller to get an array, then you’ll have to return an array, which you currently don’t: all three
return
statements return a number, not an array.The first thing to ensure is that you always return an array. But then realise that you cannot just add up the two return values you get from the recursive calls, since they are now … arrays!
You’ll also not make two recursive calls, but just one (with
n-1
), as it will return you the array that also includes then-2
entry.Finally, make sure that you just have one array that is shared by all recursive calls — so you’d pass it as argument. You can create that array once by using the default value for the second argument.
For instance:
All in all, this is rather clumsy: as you know you have to populate the array incrementally anyway, using a recursion stack is just wasted space. Just go for the iterative version.
In case you’re interested in higher numbers and you explicitly want to use recursion (although it might take a long time for computing the output), none of the other answers so far work since both produce a
RangeError
. What you want then is called trampolining and you need a bit more "machinery" for it, however it enables you to call tail recursive functions in a stack safe manner.Below is a simple implementation and a working example: