From 13202cbdd786686c7561a1a3e94efe7b5569bac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AA=E6=B5=AA=E6=B5=AA?= <675483520@qq.com> Date: Mon, 13 Aug 2018 09:36:54 +0800 Subject: [PATCH 1/3] Update initializeArrayWithRange sinppets Array.from(arrayLike[, mapFn[, thisArg]]) instand of `Array.map()` to fill with the desired values in a range --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index f5f9f435a9..269a6a26f 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) / step) }).map((v, i) => i * step + start); + Array.from({length: Math.ceil((end - start + 1) / step)}, (v, i) => i * step + start); ``` ```js From 759085acea45d00ee6af136bacc990386b0072b2 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Aug 2018 08:51:50 +0300 Subject: [PATCH 2/3] 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 269a6a26f..5aecaa34a 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 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`. From b0c1cb95e0c363266b8d8345451e3153d81f2f6b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Aug 2018 14:22:05 +0300 Subject: [PATCH 3/3] Updated tests --- test/initializeArrayWithRange/initializeArrayWithRange.js | 2 +- .../initializeArrayWithRange.test.js | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/initializeArrayWithRange/initializeArrayWithRange.js b/test/initializeArrayWithRange/initializeArrayWithRange.js index 911f6d198..e29b1c842 100644 --- a/test/initializeArrayWithRange/initializeArrayWithRange.js +++ b/test/initializeArrayWithRange/initializeArrayWithRange.js @@ -1,3 +1,3 @@ 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); module.exports = initializeArrayWithRange; diff --git a/test/initializeArrayWithRange/initializeArrayWithRange.test.js b/test/initializeArrayWithRange/initializeArrayWithRange.test.js index 8d0febed9..9530d5987 100644 --- a/test/initializeArrayWithRange/initializeArrayWithRange.test.js +++ b/test/initializeArrayWithRange/initializeArrayWithRange.test.js @@ -4,6 +4,12 @@ const initializeArrayWithRange = require('./initializeArrayWithRange.js'); test('initializeArrayWithRange is a Function', () => { expect(initializeArrayWithRange).toBeInstanceOf(Function); }); -test('Initializes an array containing the numbers in the specified range', () => { +test('Initializes an array containing the numbers in the specified range (witout start value)', () => { expect(initializeArrayWithRange(5)).toEqual([0, 1, 2, 3, 4, 5]); }); +test('Initializes an array containing the numbers in the specified range', () => { + expect(initializeArrayWithRange(7,3)).toEqual([3, 4, 5, 6, 7]); +}); +test('Initializes an array containing the numbers in the specified range (with step)', () => { + expect(initializeArrayWithRange(9,0,2)).toEqual([0, 2, 4, 6, 8]); +});