Created
June 14, 2019 09:07
-
-
Save tho-graf/b2a9bb688a02d7b15e27a42e1902a6c7 to your computer and use it in GitHub Desktop.
range-generator
This file contains 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
function* range(fromInclusive, toExclusive, step = 1) { | |
if (toExclusive == undefined) { | |
toExclusive = fromInclusive; | |
fromInclusive = 0; | |
} | |
for (let i = fromInclusive; (toExclusive - i) * step > 0; i += step) { | |
yield i; | |
} | |
} | |
console.log([...range(0, 10, 4)]); | |
console.log([...range(20, 10, -2)]); | |
console.log([...range(-10, 0)]); | |
console.log([...range(-10)]); | |
console.log([...range(0, 10)]); | |
console.log([...range(10)]); | |
for (const i of range(0, 10)) { | |
console.log(i); | |
} | |
function* range(fromInclusive, toExclusive, step = 1) { | |
if (toExclusive == undefined) { | |
toExclusive = fromInclusive; | |
fromInclusive = 0; | |
} | |
for (let i = fromInclusive; (toExclusive - i) * step > 0; i += step) { | |
yield i; | |
} | |
} | |
console.log([...range(0, 10, 4)]); | |
console.log([...range(20, 10, -2)]); | |
console.log([...range(-10, 0)]); | |
console.log([...range(-10)]); | |
console.log([...range(0, 10)]); | |
console.log([...range(10)]); | |
for (const i of range(0, 10)) { | |
console.log(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment