0

Currently learning JS.

Can't figure out at what point in this function "prev1" parameter somehow modified during recursion? If "console.log(prev1)" on each iteration "prev1" is actually modifies, although in none of the code below seems to change this parameter. Please, help me understand this thing.

        function fibonacci(n, prev1, prev2){
          //console.log(prev1);
          var current = prev1 + prev2;
          var fibonacci_string = current + " ";

          if(n > 1)
              fibonacci_string += fibonacci(n - 1, current, prev1);
          return fibonacci_string;

        }
        console.log(fibonacci(10, 1, 0));

console.log(prev1):

1
1
2
3
5
8
13
21
34
55

5 Answers5

2

If you look at the recursive call:

           fibonacci(n - 1, current, prev1)
  // calls:          ⬇️      ⬇️      ⬇️
  function fibonacci(n,     prev1,   prev2)

you can see that prev1 gets the value of the previous current.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    ok, I think i got it. On each call of the functions params (prev1 and prev2) change each other, at first prev1 = 1; prev2= 0, after first IF it goes prev1=current=1; prev2=prev1=1; on second call so on if prev1=current=2; prev2=prev1=1; etc. a bit confusing but I think I will understand fully in time :) – Nikita Moiseev Jan 12 '19 at 11:14
0

prev1 is an argument to the function fibonacci. When the line

fibonacci_string += fibonacci(n - 1, current, prev1);

is called current takes place of prev1 and is provided as argument to the fibonacci function. prev1 is not an actual variable, its just a placeholder for the value that is going to be received by the function. If in place of prev1 you give value as 5 inside the function prev1 will have a value of 5 and treated accordingly, in the above code, current is taking place of prev1 when it is called recursively and how value of current is changed you can see that.

ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

As you can see in your if statement there is another call of fibonacci() with 2nd parameter current which is the sum of prev1 and prev2. So basically in this if statement when you call fibonacci() it enters in the same function again so the code goes to the definition of function which is function fibonacci(n, prev1, prev2) where prev1 stands for the 2nd passed parameter from wherever you called this function: in this case the 2nd parameter you passed was current.

rollingthedice
  • 1,095
  • 8
  • 17
0

prev1 is a parameter of the recursive function. Each time the function is called, new execution context is created with it's own set of identifiers. Identifiers registration implies binding of values passed to the function call (arguments) with function parameters. In the provided example the invocation happens at:

fibonacci(n - 1, current, prev1);

prev1 is the second parameter of fibonacci function therefore it is bound to the second argument passed to an invocation (value of current variable).

antonku
  • 7,377
  • 2
  • 15
  • 21
0

It's a recursive function, it each iteration the parameters have different values: In the first iteration prev1=1, prev2=0 Second iteration: prev1=prev1+prev2=1, prev2=prev1=1 Third iteration: prev1=prev1+prev2=2, prev2=prev1=1 Fourh iteration: prev1=prev1+prev2=3, prev2=prev1=2 Fifth iteration: prev1=prev1+prev2=5, prev2=prev1=3

María
  • 191
  • 1
  • 5