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

This still failing for me Please help!

I am total lost on this

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 );    fib(count);
};

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

0 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

975ff1634e678a496275ad9055357f3b?s=140&d=retro ps3 over 4 years ago

Awsomeeeee Thank you so much!!!!!!

527677fcabf82128d50042ae_584313652 Tatjana Seketa over 4 years ago

Your welcome.

51c6f41b282ae3489501b5b5_435320395 William Platt about 4 years ago

That's not right, count is just used to see how efficient the function is (as the comments say); you're not supposed to use it in the function (other than incrementing it). Your method will always call fib(1) in the recursive case, then return 1. The recursive case should actually be return fib(x - 1) + fib(x - 2);.


0 votes

permalink

The recursive case should return

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

1732 points
Picture
Submitted by
Benjamin Scherer
over 4 years ago