0
i dont understand this javascript result. 😖
i dont understand the result. can someone pls explain to me where this result is coming from? function fib(num) { if(num<=2) return 1; return fib(num-1)+fib(num-2); } document.write(fib(6)); //i think that means: return 1 as long as num<=2. that is right i think. in this scope the return value is 1. //result is 8. —> but why? 6-5 + 6-2= 1+4= 5 !!???!! i dont understand it. 😟
5 ответов
+ 2
| fib(2) + fib(1) |
| fib(3) + fib(2) |
| fib(4) + fib(3) |
| fib(5) + fib(4) |
------------------------
Consider the above as a stack.
So the call on the top both become 1 and they return the same value as they pop out of the stack one by one.
So probably 8.
Please correct me if I'm wrong.
+ 1
Are you not missing an else after return 1? Just asking.
+ 1
It is just recursion so until your num becomes less than or equal to 2, no value will be returned and the fib() will be called again and again.
0
that question is out of a js challenge. i just copied. but yes, i also thought that the else was missing....
0
does it mean i return the amount of the fib and not the value?if so, why the amount or number of fibs is not 4?
the right answer is 8, you are right. 👍