fix/update intializeArrayWithRange.md
This commit is contained in:
@ -1,12 +1,13 @@
|
|||||||
### initializeArrayWithRange
|
### initializeArrayWithRange
|
||||||
|
|
||||||
Initializes an array containing the numbers in the specified range.
|
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive.
|
||||||
|
|
||||||
Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
|
Use `Array(end-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`.
|
You can omit `start` to use a default value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const initializeArrayWithRange = (end, start = 0) =>
|
const initializeArrayWithRange = (end, start = 0) =>
|
||||||
Array.from({ length: end - start }).map((v, i) => i + start);
|
Array.from({ length: (end + 1) - start }).map((v, i) => i + start);
|
||||||
// initializeArrayWithRange(5) -> [0,1,2,3,4]
|
// initializeArrayWithRange(5) -> [0,1,2,3,4,5]
|
||||||
|
// initializeArrayWithRange(7, 3) -> [3,4,5,6,7]
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user