From b983380559eb2d921090262e748d6723e4787a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Ogam?= Date: Wed, 9 Oct 2024 14:52:25 +0200 Subject: [PATCH 1/2] Make range upper bound exclusive --- .../reference/global_objects/array/from/index.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/files/en-us/web/javascript/reference/global_objects/array/from/index.md b/files/en-us/web/javascript/reference/global_objects/array/from/index.md index 70728c985b87829..87f4a032c4ef272 100644 --- a/files/en-us/web/javascript/reference/global_objects/array/from/index.md +++ b/files/en-us/web/javascript/reference/global_objects/array/from/index.md @@ -132,20 +132,20 @@ Array.from({ length: 5 }, (v, i) => i); ### Sequence generator (range) ```js -// Sequence generator function (commonly referred to as "range", e.g. Clojure, PHP, etc.) +// Sequence generator function (commonly referred to as "range", e.g. Python, Clojure, etc.) const range = (start, stop, step) => - Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step); + Array.from({ length: Math.ceil((stop - start) / step - 1) + 1 }, (_, i) => start + i * step); -// Generate numbers range 0..4 -range(0, 4, 1); +// Generate numbers from 0 (inclusive) to 4 (exclusive) with step 1 +range(0, 5, 1); // [0, 1, 2, 3, 4] -// Generate numbers range 1..10 with step of 2 +// Generate numbers from 1 (inclusive) to 10 (exclusive) with step 2 range(1, 10, 2); // [1, 3, 5, 7, 9] -// Generate the alphabet using Array.from making use of it being ordered as a sequence -range("A".charCodeAt(0), "Z".charCodeAt(0), 1).map((x) => +// Generate the alphabet making use of it being ordered as a sequence +range("A".charCodeAt(0), "Z".charCodeAt(0) + 1, 1).map((x) => String.fromCharCode(x), ); // ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] From 68ca483e4cde6b1eb25cb04ae57128fad4a4dca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Ogam?= Date: Wed, 9 Oct 2024 15:08:48 +0200 Subject: [PATCH 2/2] Lint Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../javascript/reference/global_objects/array/from/index.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/files/en-us/web/javascript/reference/global_objects/array/from/index.md b/files/en-us/web/javascript/reference/global_objects/array/from/index.md index 87f4a032c4ef272..ab243608b3d2a8e 100644 --- a/files/en-us/web/javascript/reference/global_objects/array/from/index.md +++ b/files/en-us/web/javascript/reference/global_objects/array/from/index.md @@ -134,7 +134,10 @@ Array.from({ length: 5 }, (v, i) => i); ```js // Sequence generator function (commonly referred to as "range", e.g. Python, Clojure, etc.) const range = (start, stop, step) => - Array.from({ length: Math.ceil((stop - start) / step - 1) + 1 }, (_, i) => start + i * step); + Array.from( + { length: Math.ceil((stop - start) / step - 1) + 1 }, + (_, i) => start + i * step, + ); // Generate numbers from 0 (inclusive) to 4 (exclusive) with step 1 range(0, 5, 1);