Created
July 8, 2022 13:01
-
-
Save eddiecorrigall/ea55ca1f793f0bb25e3073e93fede76e to your computer and use it in GitHub Desktop.
Ideas for JavaScript helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const range = (end, start) => { | |
// Return an Array of a sequence of integers from start to end-1. | |
// Let end be an non-negative integer, which represent the last number in a sequence exclusively. | |
// Let start be an (optional) integer (zero, negative or positive), which represents the first number in a sequence inclusively. | |
// Examples: | |
// range(4) produces [0, 1, 2, 3] | |
// range(4, -3) produces [-3, -2, -1, 0, 1, 2, 3] | |
if (end === undefined) return []; | |
if (end < 0) return []; | |
return Array(end - (start ?? 0)) | |
.fill() | |
.map((_, index) => start + index); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment