Skip to content

Instantly share code, notes, and snippets.

@eddiecorrigall
Created July 8, 2022 13:01
Show Gist options
  • Save eddiecorrigall/ea55ca1f793f0bb25e3073e93fede76e to your computer and use it in GitHub Desktop.
Save eddiecorrigall/ea55ca1f793f0bb25e3073e93fede76e to your computer and use it in GitHub Desktop.
Ideas for JavaScript helpers
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