Skip to content

Instantly share code, notes, and snippets.

@siberex
Last active August 21, 2025 16:37
Show Gist options
  • Save siberex/e5b4714163c22f874b9fa1b2058ffc1f to your computer and use it in GitHub Desktop.
Save siberex/e5b4714163c22f874b9fa1b2058ffc1f to your computer and use it in GitHub Desktop.
JS range Python-style
function* range(start, end, skip = 1) {
if (!end) {end = start; start = 1}
while (start <= end) {
yield start;
start += skip
}
}
console.log( [...range(7)] );
// [ 1, 2, 3, 4, 5, 6, 7 ]
console.log( [...range(3, 10, 2)] );
// [ 3, 5, 7, 9 ]
console.log(
[...range(1, 201, 2)]
.reduce((sum, el,i) => sum += !(i % 10) ? el : 0, 0)
);
// 1111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment