--- title: Fibonacci sequence id: 597f24c1dda4e70f53c79c81 challengeType: 5 isHidden: false forumTopicId: 302268 --- ## Description
Write a function to generate the nth Fibonacci number. The nth Fibonacci number is given by: Fn = Fn-1 + Fn-2 The first two terms of the series are 0 and 1. Hence, the series is: 0, 1, 1, 2, 3, 5, 8, 13...
## Instructions
## Tests
```yml tests: - text: fibonacci should be a function. testString: assert(typeof fibonacci === 'function'); - text: fibonacci(2) should return a number. testString: assert(typeof fibonacci(2) == 'number'); - text: fibonacci(3) should return 2. testString: assert.equal(fibonacci(3),2); - text: fibonacci(5) should return 5. testString: assert.equal(fibonacci(5),5); - text: fibonacci(10) should return 55. testString: assert.equal(fibonacci(10),55); ```
## Challenge Seed
```js function fibonacci(n) { // Good luck! } ```
## Solution
```js function fibonacci(n) { let a = 0, b = 1, t; while (--n >= 0) { t = a; a = b; b += t; } return a; } ```