Skip to content

Instantly share code, notes, and snippets.

@jportella93
Created January 31, 2019 19:47
Show Gist options
  • Save jportella93/2b7a749c4b2fee1e9e6ea7c7e8205932 to your computer and use it in GitHub Desktop.
Save jportella93/2b7a749c4b2fee1e9e6ea7c7e8205932 to your computer and use it in GitHub Desktop.
Create a range between to numbers in array form, with optional steps.
const range = (b, e = (b + (b = 0)), s = 1) =>
Array.from({ length: (e - b) / s + 1 }, (v, i) => i * s + b);
// range(beggining, end, step)
// range(10) === [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// range(-5, 5) === [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
// range(-10, 10, 5) === [-10, -5, 0, 5, 10]
// range(10, 5) === []
// range(10, 5, -1) === [10, 9, 8, 7, 6, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment