2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
title: Fibonacci sequence
|
|
|
|
id: 597f24c1dda4e70f53c79c81
|
|
|
|
challengeType: 5
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
<p>Write a function to generate the <big> n<sup>th</sup> </big> Fibonacci number.</p>
|
|
|
|
///<p>The <big> n<sup>th</sup> </big> Fibonacci number is given by :
|
|
|
|
///<p>F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub></p>
|
|
|
|
///<p>The first two terms of the series are 0, 1.</p>
|
|
|
|
///<p>Hence, the series is : 0, 1, 1, 2, 3, 5, 8, 13...</p>
|
|
|
|
///
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
- text: <code>fibonacci</code> is a function.
|
2018-10-02 15:02:53 +01:00
|
|
|
testString: 'assert(typeof fibonacci === ''function'', ''<code>fibonacci</code> is a function.'');'
|
2018-09-30 23:01:58 +01:00
|
|
|
- text: <code>fibonacci(2)</code> should return a number.
|
2018-10-02 15:02:53 +01:00
|
|
|
testString: 'assert(typeof fibonacci(2) == ''number'', ''<code>fibonacci(2)</code> should return a number.'');'
|
2018-09-30 23:01:58 +01:00
|
|
|
- text: <code>fibonacci(3)</code> should return 1.")
|
|
|
|
testString: 'assert.equal(fibonacci(3),1,"<code>fibonacci(3)</code> should return 1.");'
|
|
|
|
- text: <code>fibonacci(5)</code> should return 3.")
|
|
|
|
testString: 'assert.equal(fibonacci(5),3,"<code>fibonacci(5)</code> should return 3.");'
|
|
|
|
- text: <code>fibonacci(10)</code> should return 34.")
|
|
|
|
testString: 'assert.equal(fibonacci(10),34,"<code>fibonacci(10)</code> should return 34.");'
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function fibonacci(n) {
|
|
|
|
// Good luck!
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
function fibonacci(n) {
|
|
|
|
let a = 0, b = 1, t;
|
|
|
|
while (--n > 0) {
|
|
|
|
t = a;
|
|
|
|
a = b;
|
|
|
|
b += t;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|