3 ms·
Not sure what the test cases are for the Fibonacci sequence (just happened to pick that one first), but it gives an error for my code, and I'm not able to figur
by stevewodil 7y ago
Not sure what the test cases are for the Fibonacci sequence (just happened to pick that one first), but it gives an error for my code, and I'm not able to figure out why. (please ignore my bad variable names)
function(index) {
var fib = [0,1];
for(var i=2; i<index; i++){
fib[i]=fib[i-2]+fib[i-1];
}
return fib[index-1];
}
- fibs34 7y agoI remember my first Fibonacci sequence
- viceroyalbean 7y agoYour function will return 2 for index = 4, whereas it's expecting 3.
- stevewodil 7y agoThen are the instructions not clear enough, or am I just missing something.... From the website: "For example, the sequence [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] forms the first ten entries of the Fibonacci sequence." Entering 4 should return 2 in this case, which is correct? I don't get it.
- fwip 7y agoIndex 4 is the fifth element, 3. Index 0 is the first element (a zero). Reread the examples.
- stevewodil 7y agoHmm...I see. Thank you for the help!