Build README

This commit is contained in:
Angelos Chalaris
2017-12-12 19:21:31 +02:00
parent 1a69ec2ea4
commit 271130ef08

View File

@ -215,15 +215,11 @@ const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
### 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.
Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two.
```js
const 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;
},[]);
const fibonacci = n =>
Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),[]);
// fibonacci(5) -> [0,1,1,2,3]
```