diff --git a/README.md b/README.md index 808d6da55..5fcf650d0 100644 --- a/README.md +++ b/README.md @@ -1553,13 +1553,13 @@ initialize2DArray(2, 2, 0); // [[0,0], [0,0]] Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with their common difference `step`. -Use `Array.from(Math.ceil((end+1-start)/step))` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.map()` to fill with the desired values in a range. +Use `Array.from()` to create an array of the desired length, `(end - start + 1)/step`, and a map function to fill it with the desired values in the given range. You can omit `start` to use a default value of `0`. You can omit `step` to use a default value of `1`. ```js const initializeArrayWithRange = (end, start = 0, step = 1) => - Array.from({ length: Math.ceil((end + 1 - start) / step) }).map((v, i) => i * step + start); + Array.from({ length: Math.ceil((end - start + 1) / step) }, (v, i) => i * step + start); ```
diff --git a/docs/array.html b/docs/array.html index 33f5477b2..90912d0ad 100644 --- a/docs/array.html +++ b/docs/array.html @@ -198,8 +198,8 @@

initialize2DArray

Initializes a 2D array of given width and height and value.

Use Array.map() to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default to null.

const initialize2DArray = (w, h, val = null) =>
   Array.from({ length: h }).map(() => Array.from({ length: w }).fill(val));
 
initialize2DArray(2, 2, 0); // [[0,0], [0,0]]
-

initializeArrayWithRange

Initializes an array containing the numbers in the specified range where start and end are inclusive with their common difference step.

Use Array.from(Math.ceil((end+1-start)/step)) to create an array of the desired length(the amounts of elements is equal to (end-start)/step or (end+1-start)/step for inclusive end), 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 step to use a default value of 1.

const initializeArrayWithRange = (end, start = 0, step = 1) =>
-  Array.from({ length: Math.ceil((end + 1 - start) / step) }).map((v, i) => i * step + start);
+

initializeArrayWithRange

Initializes an array containing the numbers in the specified range where start and end are inclusive with their common difference step.

Use Array.from() to create an array of the desired length, (end - start + 1)/step, and a map function to fill it with the desired values in the given range. You can omit start to use a default value of 0. You can omit step to use a default value of 1.

const initializeArrayWithRange = (end, start = 0, step = 1) =>
+  Array.from({ length: Math.ceil((end - start + 1) / step) }, (v, i) => i * step + start);
 
initializeArrayWithRange(5); // [0,1,2,3,4,5]
 initializeArrayWithRange(7, 3); // [3,4,5,6,7]
 initializeArrayWithRange(9, 0, 2); // [0,2,4,6,8]
diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md
index 5aecaa34a..4cac19380 100644
--- a/snippets/initializeArrayWithRange.md
+++ b/snippets/initializeArrayWithRange.md
@@ -8,7 +8,7 @@ You can omit `step` to use a default value of `1`.
 
 ```js
 const initializeArrayWithRange = (end, start = 0, step = 1) =>
-  Array.from({length: Math.ceil((end - start + 1) / step)}, (v, i) => i * step + start);
+  Array.from({ length: Math.ceil((end - start + 1) / step) }, (v, i) => i * step + start);
 ```
 
 ```js