Home > Software design >  Fibonacci sequence generator not working for 1 or 2 as inputs but it works for the rest of the seque
Fibonacci sequence generator not working for 1 or 2 as inputs but it works for the rest of the seque

Time:03-23

Can someone explain to me why my fibonacciGenerator function doesn't work with this code? I understand why it works with the second code tho but I just don't get why the first one doesn't.

function fibonacciGenerator(n) {

  if (n > 0) {
    var fArray = [];
    fArray.push(0);

    if (n >= 2) {
      fArray.push(1);
    }
    for (var i = 0; i < n; i  ) {
      fArray.push(fArray[i]   fArray[i   1]);
    }
    console.log(fArray);
  }
}

fibonacciGenerator(1);
fibonacciGenerator(2);

Second code working :

function fibonacciGenerator(n) {

  if (n > 0) {
    var fArray = [];
    fArray.push(0);

    if (n >= 2) {
      fArray.push(1);
    }
    for (var i = 2; i < n; i  ) {
      fArray.push(fArray[i - 1]   fArray[i - 2]);
    }
    console.log(fArray);
  }
}

fibonacciGenerator(1);
fibonacciGenerator(2);

CodePudding user response:

The first code is printing 2 extra Fibonacci number this is because:

you are first pushing 0 and 1 into the array as:

var fArray = [];
    fArray.push(0);
  
    if (n >=2 ){
        fArray.push(1);
    }

and then you loop over again till n times. Because of this reason it prints two extra Fibonacci numbers.

the solution is to either loop over n-2 time or to use the second code.

CodePudding user response:

var fArray = [];
fArray.push(0);

if (n >= 2) {
  fArray.push(1);
}

The initial condition is to cover n=1: [0] and n=2: [0,1]

The 2nd code is working because the loop only starts when n is greater than i, so means it skips the loop with n < 2.

For your problem, you don't skip the loop when n < 2.

for (var i = 0; i < n; i  ) {
  fArray.push(fArray[i]   fArray[i   1]);
}

You can imagine the result will be like below when n < 2 with your loop. Note that the inital value is fArray = [0]

fArray.push(fArray[0]   fArray[1]); //fArray[1] is undefined because you only have 1 item in your array

In this case fArray[0] fArray[1] ==> 0 undefined = NaN

So that's why your logic does not work when n < 2

To correct it, you need to avoid the loop if n < 2

//if n=1 or n=2, it won't trigger the loop due to `i < n-2`
for (var i = 0; i < n-2; i  ) {
   fArray.push(fArray[i]   fArray[i   1]);
}

CodePudding user response:

The idea to have i start with 0 instead of 2, and to adjust the body of the loop accordingly, is fine, but there is one thing that the first version didn't adjust: the stop condition of the loop.

By setting i=0, the first version loops 2 times more than the second version. You should also alter the end condition in the same way: instead of i < n, it should have i < n - 2, so to ensure the number of iterations is the same as in the second version.

Not related to your question, but the console.log should better be placed outside of the function. The job of the function should be to return the array, not to print it. So also, when n > 0 is false, it should return an empty array.

function fibonacciGenerator(n) {

  var fArray = [];
  if (n > 0) {
    fArray.push(0);

    if (n >= 2) {
      fArray.push(1);
    }
    for (var i = 0; i < n - 2; i  ) {
      fArray.push(fArray[i]   fArray[i   1]);
    }
  }
  return fArray;
}

console.log(fibonacciGenerator(1));
console.log(fibonacciGenerator(2));

CodePudding user response:

First you have to identify the pattern.

Fibonacci series -> 0 1 1 2 3 5 8 13 21

Term             -> 0 1 2 3 4 5 6 7  8


0th term =0, 1st term =1

From the second term,

2nd = 1st term   0th term = 1 0 = 1
3rd = 2nd term   1st term = 1 1 = 2
4th = 3rd term   2nd term = 2 1 = 3
5th = 4th term   3rd term = 3 2 = 5
nth = (n-1)   (n-2)

since the first 2 terms are fixed, you have to start for loop from i= 2. Also, according to the above shown pattern, you have to use following code inside the for loop.

fArray.push(fArray[i - 1]   fArray[i-2]);
  • Related