Last active
March 13, 2020 13:40
-
-
Save cevherkarakoc/ccabf9b84e5234457081eadaf4d5583c to your computer and use it in GitHub Desktop.
a range function
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 = (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