-2
const yourself = {
count: 0,
fibonacci(n) {
this.count++;
**console.log(`${this.count}º vez - ${n}`);**
if (n === 0 || n === 1) {
return n;
} else {
return this.fibonacci(n - 1) + this.fibonacci(n - 2);
}
}
};
yourself.fibonacci(2);
Output from script 2 1 0, my wish 0 1 2, so I can better view the recursive stack structure where the last value of the call is the first to be released.
Instead of starting at zero and incrementing to 1, start at 1 and descend (
count--
) to zero.– G. Bittencourt
I edited, gave a bad example.
– Gustavo
But in this case the question is not simply to reverse the order, you will have to change your logic a little. Either insert the results into a list or calculate the values iteratively.
– G. Bittencourt
You are printing calls in order of stacking. What you are not able to view?
– bfavaretto
that your code will not give this exit 2 1 0 and yes the 1 1
– user60252
use Return arr.Reverse();
– user60252
I think the title of the question does not match the problem of fact.
– G. Bittencourt
@G.Bittencourt Thanks for the touch, I made a list, then I think something better.
– Gustavo