2

So I created the fibonacci series, but the way I found on w3resources is a bit confusing to me as a beginner. What exactly happens when n = 2? s becomes fibonnacci_series(1), but what does this even imply? In the first place why do we have var fibonacci_series = function(n) , why not just have a function called function fibonacci_series(n)?

var fibonacci_series = function (n)   
{  
  if (n===1)   
  {  
    return [0, 1];  
  }   
  else   
  {  
    var s = fibonacci_series(n - 1);  
    s.push(s[s.length - 1] + s[s.length - 2]);  
    return s;  
  }  
};  

 console.log(fibonacci_series(8));
Jason Genova
  • 127
  • 7

3 Answers3

2

These two lines do almost the same thing (see here for more details but as said in the comments, this is probably not relevant for you right now):

var fibonacci_series = function (n)   

function fibonacci_series(n)

Both define a function called fibonacci_series that take n as parameter, it's just a variant.

The fibonacci series function is a recursive function, it means that it calls itself in its body (e.g there is fibonnacci_series(n-1) inside the function).

When n = 2, s equals the value of fibonnacci_series(1), which is what is returned by the if statement.

Community
  • 1
  • 1
Graham Slick
  • 6,692
  • 9
  • 51
  • 87
  • 4
    There are actually some subtle differences, but they are probably irrelevant for the OPs question. See [for example](http://stackoverflow.com/a/5857498/1250301) – Matt Burland Jun 09 '16 at 19:30
2

When n = 2, s becomes the result of the recursive call of n - 1 (which is [0,1]). Then, s.push(s[s.length - 1] + s[s.length - 2]) is pushing the addition of the 2nd last element with the last element, which is how the fibonacci sequence is obtained.

This is a reccursive implementation of the fibonacci sequence. It can also be done through iteration.

Chris Wissmach
  • 505
  • 4
  • 11
  • When s = 2, s = fibonacci_series(1). What does this mean? I dont understand what fibonacci_series(1) exactly means. Does this just mean s = [0,1]? – Jason Genova Jun 09 '16 at 19:37
  • Basically, yes. The statement `s = fibonacci_series(n - 1)` is a recursive call. Since `n = 2` in this case, the function will be called again with `n = 1`. If you look at the return value for the case when `n = 1`, you'll see that it returns `[0,1]`. Thus, `s = [0,1]`. – Chris Wissmach Jun 09 '16 at 19:44
1

What exactly happens when n = 2? s becomes fibonacci_series(1), but what does this even imply?

This means you're going to get the same result as if you had called fibonacci_series(1) by itself. So s will be [0, 1]. Afterwards, you add the sum of those two numbers to the end of the array, resulting in [0, 1, 1]. This relationship (Nm = N(m-1) + N(m - 2)) is what defines the Fibonacci sequence.

Writing it in the this way means that this same process will happen recursively. Following this logic, try writing down what would happen if n = 3. What would happen during the first call to fibonacci_series? How would the next call to fibonacci_series change what the function returns?

In the first place why do we have var fibonacci_series = function(n), why not just have a function called function fibonacci_series(n)?

For your purposes, there's really no difference. The technical answer is that the first is a function expression and the second is a function declaration. If you're interested in the details of how they're different, check out this Stack Overflow question.

Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • 1
    wow. This thing suddenly makes all sense. It was really creative how they thought of using .length. Honestly would have never thought of that. I was just doing it iteratively before. Thanks so much – Jason Genova Jun 09 '16 at 19:40
  • @Munt No worries! Not going to lie, I remember this thing blowing my mind when I first learned about it. – Mike Cluck Jun 09 '16 at 19:41