Describe your problem and how to reproduce it:
There is an off by one error in the challenge description vs the test suite for this problem. According to the challenge description (and all of the official descriptions of the sequence https://oeis.org/A000045), the Fibonacci sequence uses zero based indexing. The test suite uses one based indexing. The test suite should be updated to reflect zero based indexing.
Add a Link to the page with the problem:
https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/fibonacci-sequence
Tell us about your browser and operating system:
If possible, add a screenshot here (you can drag and drop, png, jpg, gif, etc. in this box):
N/A
@jeremylt I could be mistaken, but no where in the challenge does it state that the argument passed to the function represents the index. Instead, it just refers to the term (position in the sequence). The 3rd term in the Fiboncci sequence would be 1. The 5th term would be 3. The 10th term would be 34. That is what the tests are checking.
The description uses F_n and nth in conjunction. If the description uses F_n, n must be zero based indexing. I understand how the tests want you to code the solution. It's just mathematically incorrect given that the description refers to F_n.
@jeremylt Would you mind highlighting what part of the link you referenced that state F_n is zero-indexed. I am unable to see what you are referring to.
Sure
"A000045 Fibonacci numbers: F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1."
F_0 and F(0) are equivalent nomenclature.
Zero based indexing is also the norm when students Google Fibonacci, so I think bringing this challenge in line with standard notation reinforces Googling for info.
@snigo Comments?
My two cents:
I believe that @jeremylt has a point. The formula described states f(0) = 1 f(1)=1 f(2) = 1 ... But the result expected is fibonacci(1) = 0 fibonacci(2) = 1 fibonacci(3) = 1 which can be a cause of confusion.
If we use the mathematical form, the solution should be:
function fibonacci(n) {
if (n==0) return 0;
if (n==1) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}
In the current form, the solution expected is:
function fibonacci(n) {
if (n==1) return 0;
if (n==2) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}
I know there is not much difference, but I remember going for the mathematical form myself when I attempted this the first time and then I had to read the hints below to understand that the index was off by 1.
I agree with 0-based indexing, so fibonacci(10)
would be 55
.
@RandellDawson I have pushed the required changes for this to be according to base 0.
Kindly review. Thanks.
I looked at formula, and some calculators implementations. they all seem to be 0 based.
Thanks everyone!
Most helpful comment
I agree with 0-based indexing, so
fibonacci(10)
would be55
.