Skip to content

Instantly share code, notes, and snippets.

@cevherkarakoc
Last active March 13, 2020 13:40
Show Gist options
  • Save cevherkarakoc/ccabf9b84e5234457081eadaf4d5583c to your computer and use it in GitHub Desktop.
Save cevherkarakoc/ccabf9b84e5234457081eadaf4d5583c to your computer and use it in GitHub Desktop.
a range function
const range = (stop, start = 0, step = 1) => (
(step === 0 || ((stop - start) * Math.sign(step)) <= 0) ?
[] :
[...new Array(Math.abs(Math.ceil((stop - start) / step)))].map((_, index) => index * step + start)
)
range(6)
// output : [0, 1, 2, 3, 4, 5]
range(20, 1, 5)
// output : [1, 6, 11, 16]
range(-6, 5, -2)
// output : [5, 3, 1, -1, -3, -5]
range(1, 5, 3)
// output : []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment