This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Big O Forum

View Course » View Exercise

2173 points
975ff1634e678a496275ad9055357f3b?s=140&d=retro
Submitted by
ps3
over 4 years ago

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+".");

3 votes

permalink

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!

3347 points
527677fcabf82128d50042ae_584313652
Submitted by
Tatjana Seketa
over 4 years ago

3 Comments

Picture Franzi Vegas over 4 years ago

Thank you, it really helped me.

527677fcabf82128d50042ae_584313652 Tatjana Seketa over 4 years ago

Your welcome.

533b22568c1ccc6c0d00409e_910858883 Sofia Perwallius about 3 years ago

Thank you a lot


0 votes

permalink

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.

201 points
50b411ec7dff47ccd2000530_416585248
Submitted by
TenebrousNight
over 4 years ago


0 votes

permalink

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+".");

461 points
Picture
Submitted by
Pete Timpone
over 3 years ago


-1 votes

permalink

Problem is you can pass this without the right answer. But what I don't understand is why you guy just didn't have:

 return fib(x-1) + fib(x-2);

2730 points
518e281cd292f87cc1001d83_947894952
Submitted by
Shahn
almost 4 years ago