Files
30-seconds-of-code/snippets/initializeArrayWithRange.md
Angelos Chalaris b74eb9d9bd Linting
2017-12-27 11:02:46 +02:00

14 lines
551 B
Markdown

### initializeArrayWithRange
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive.
Use `Array((end + 1) - start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
You can omit `start` to use a default value of `0`.
```js
const initializeArrayWithRange = (end, start = 0) =>
Array.from({ length: (end + 1) - start }).map((v, i) => i + start);
// initializeArrayWithRange(5) -> [0,1,2,3,4,5]
// initializeArrayWithRange(7, 3) -> [3,4,5,6,7]
```