In mathematical terms, the Fibonacci sequence is a series of numbers in which each number is the sum of the two previous numbers. The sequence *F _{n}* is defined by:

*F _{n} = F_{n-1} + F_{n-2}*, with seed values

*F*

_{1}= 1, F_{2}= 1**or**

*F*.

_{0}= 0, F_{1}= 1So, for example, assuming seed values of 0 and 1, the sequence may look like:

0, 1, 1, 2, 3, 5, 8, 13

In this example, the result for F_{7} is 13.

Logically, the javascript code should look something like this:

*var myFib2 = function (n) {*

* if (n <= 2) { return 1; }*

* else { return myFib2(n – 1) + myFib2(n – 2); }*

* };*

This works but the code is inefficient with large indices. A more efficient solution is:

*var myFib = function (n) {*

* var curr_n;*

* var f = function (b, a) {*

* var x = curr_n === 1? 1: (a || 0) + b;*

* return curr_n++ < n? f(x, b): x;*

* };*

* return (f(curr_n = 0));*

* };*

Thanks to my friend SB for assisting with the improved solution. Check out the fiddle here.