Update fibonacci-array-generator.md
This commit is contained in:
@ -1,16 +1,10 @@
|
|||||||
### Fibonacci array generator
|
### Fibonacci array generator
|
||||||
|
|
||||||
Create an empty array of the specific length, initializing the first two values (`0` and `1`).
|
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
|
```js
|
||||||
const fibonacci = n => {
|
const fibonacci = n =>
|
||||||
return Array(n)
|
Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),[]);
|
||||||
.fill(0)
|
|
||||||
.reduce((acc, val, i) => {
|
|
||||||
acc.push(i > 1 ? acc[i - 1] + acc[i - 2] : i);
|
|
||||||
return acc;
|
|
||||||
},[]);
|
|
||||||
}
|
|
||||||
// fibonacci(5) -> [0,1,1,2,3]
|
// fibonacci(5) -> [0,1,1,2,3]
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user