Created
June 27, 2020 17:07
-
-
Save jeffjohnson9046/8ff5091515b5dd8d0f22243fc7ac673d to your computer and use it in GitHub Desktop.
A Javascript generator to generate a range of numbers
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
// Source: | |
// https://dev.to/ycmjason/how-to-create-range-in-javascript-539i | |
function* range(start, end) { | |
yield start; | |
if (start === end) { | |
return; | |
} | |
yield* range(start + 1, end); | |
} | |
// usage | |
[...range(0, 8)].map(s => console.log('I am ' + s)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment