Fibonacci and random

This commit is contained in:
Angelos Chalaris
2017-11-30 19:45:56 +02:00
parent d9c4f9234f
commit 0893f79360
4 changed files with 62 additions and 2 deletions

View File

@ -0,0 +1,13 @@
### Fibonacci array generator
Create an empty array of the specific length, initializing the first two values (`0` and `1`).
Use `reduce()` to add values into the array, using the sum of the last two values, except for the first two.
```js
var fibonacci = n =>
Array.apply(null, [0,1].concat(Array(n-2))).reduce(
(acc, val, i) => {
acc.push( i>1 ? acc[i-1]+acc[i-2] : val);
return acc;
},[]);
```