From dde371f8f9ad44c20c26daea3211fe4d9895a950 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 c9e46a1431a2a0ca773b0353da7234de009fc03d 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 1cb12e7db1b85151013ddd50b20c9718b9e498de 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 37960658bfd2b7c503684ad57a3328cd2507aee5 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 07f3d1167e96574a019c697e0e10282477524b5c 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`.