From 88c8c3596ea5660f3a5078d0cb680272647456d2 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 12:30:55 +0530 Subject: [PATCH 1/5] Update InitializeArrayWithRange.md Update InitializeArrayWithRange.md to include another parameter step --- snippets/initializeArrayWithRange.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 22b126ad8..ba68e3b2c 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -1,16 +1,18 @@ ### initializeArrayWithRange -Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive. +Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with there common difference `step`. -Use `Array((end + 1) - start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. +Use `Array(Math.ceil((end + 1) - start)/2)` 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 `step` to use a default value of `1`. + ```js -const initializeArrayWithRange = (end, start = 0) => - Array.from({ length: end + 1 - start }).map((v, i) => i + start); +const initializeArrayWithRange = (end, start = 0,step = 1) => + Array(Math.ceil((end + 1 - start)/2)).fill(0).map((v, i) => (i * step) + start); ``` ```js 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] ``` From 83f6a7f4e7b07fb3f87bb09b002c34b3e52a9971 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 13:11:37 +0530 Subject: [PATCH 2/5] Update initializeArrayWithRange.md --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index ba68e3b2c..2b3c323a1 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(Math.ceil((end + 1 - start)/2)).fill(0).map((v, i) => (i * step) + start); + Array.from({length:Math.ceil((end + 1 - start)/2))}).map((v, i) => (i * step) + start); ``` ```js From c137dd5277eaa34d4b348a44766d0f7fdab35742 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 14:38:42 +0530 Subject: [PATCH 3/5] Update initializeArrayWithRange.md --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 2b3c323a1..38945c1b3 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 + 1 - start)/2))}).map((v, i) => (i * step) + start); + Array.from({length:Math.ceil((end + 2 - start - step)/step)}).map((v, i) => (i * step) + start); ``` ```js From 3c4b21ca0f9b8e697fb4076cdd50d4be14880e40 Mon Sep 17 00:00:00 2001 From: David Wu Date: Wed, 3 Jan 2018 12:42:11 +0100 Subject: [PATCH 4/5] Fix end inclusive error --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 38945c1b3..53cf27d4d 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 + 2 - start - step)/step)}).map((v, i) => (i * step) + start); + Array.from({length:Math.ceil((end + 1 - start)/step)}).map((v, i) => (i * step) + start); ``` ```js From 5dfd0602021f28ec4ff427779c627fb2750836da Mon Sep 17 00:00:00 2001 From: David Wu Date: Wed, 3 Jan 2018 12:45:09 +0100 Subject: [PATCH 5/5] Update description --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 53cf27d4d..7c803aed0 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -2,7 +2,7 @@ Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with there common difference `step`. -Use `Array(Math.ceil((end + 1) - start)/2)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. +Use `Array(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`.