--- title: Fibonacci sequence id: 597f24c1dda4e70f53c79c81 localeTitle: 597f24c1dda4e70f53c79c81 challengeType: 5 --- ## Description

Escribe una función para generar el n º número de Fibonacci.

///

El número n de Fibonacci viene dado por: ///

F n = F n-1 + F n-2

///

Los dos primeros términos de la serie son 0, 1.

///

Por lo tanto, la serie es: 0, 1, 1, 2, 3, 5, 8, 13 ...

///
## Instructions
## Tests
```yml tests: - text: fibonacci es una función. testString: 'assert(typeof fibonacci === "function", "fibonacci is a function.");' - text: fibonacci(2) debe devolver un número. testString: 'assert(typeof fibonacci(2) == "number", "fibonacci(2) should return a number.");' - text: fibonacci(3) debe devolver 1. ") testString: 'assert.equal(fibonacci(3),1,"fibonacci(3) should return 1.");' - text: fibonacci(5) debe devolver 3. ") testString: 'assert.equal(fibonacci(5),3,"fibonacci(5) should return 3.");' - text: fibonacci(10) debe devolver 34. ") testString: 'assert.equal(fibonacci(10),34,"fibonacci(10) should return 34.");' ```
## 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; } ```