# Big O 2.5 I am totally lost to what to do here

I am totally lost to what to do here

var count = 0;

// Recursively find Xth Fibonacci number
var fib = function(x){
// Keeps track of the amount of work done computing a Fibonacci
count++;

// Base Case
if(x < 10  ) {    }

// Recursive calls
return fib( x )    fib(  );
}

var f = fib(10);
console.log("It took "+count+" calculations to find that the 10th fibonacci number is "+f+".");

I found your problem! All you have to do is take away the fib(count); from after the return function!

// here is the finished code
var count = 0;

// Recursively find Xth Fibonacci number
var fib = function(x){

// Keeps track of the amount of work done computing a Fibonacci
count++;

// Base Case
if(x < 2  ) { return 1; }

// Recursive calls
return fib( count );
};

var f = fib(10);

console.log("It took "+count+" calculations to find that the 10th fibonacci number is "+f+".");

Hope this helped!

Franzi Vegas over 4 years ago

Thank you, it really helped me.

Tatjana Seketa over 4 years ago

Sofia Perwallius about 3 years ago

Thank you a lot

I'll start you off by giving the base case. If you know the first few values of a Fibonacci series (1, 1, 2), you notice that there's a single point where you can stop asking for the preceding numbers. Since the first two are always 1, when fib(2) is called, it should return 1 without having to look further back (the same goes for fib(1)). So your base case should look something like this:

if (x <= 2) { return 1; }

This isn't required by the problem, but as an extra measure, I would also add a clause for values of n <= 0, just so that the 0th term of the fibonacci sequence isn't returned as being 1! So mine looks like this:

if (x <= 0) { return 0; }
else if (x <= 2) { return 1; }

Like I mentioned, this second part isn't required by the assignment, but it's just something to consider.

Now I'm going to let you figure the part about the recursive calls. Just remember that each time you want to know the nth Fibonacci term, you need to know the preceding two.

Here's the correct answer from start to finish. If you want to figure out the problem yourself this will spoil it!!

var count = 0;

// Recursively find Xth Fibonacci number
var fib = function(x){
// Keeps track of the amount of work done computing a Fibonacci
count++;

// Base Case
if (x<=0) {return 0;}//**Optional** Thanks, TenebrousNight :D
if(x<=2){return 1;}
// Recursive calls
return fib(x - 1) + fib(x - 2);
};

var f = fib(10);
console.log("It took "+count+" calculations to find that the 10th fibonacci number is "+f+".");