Skip to content

Instantly share code, notes, and snippets.

@Ayc0
Last active December 23, 2018 11:43
Show Gist options
  • Save Ayc0/93caeb7195741a497d1e25a111e95325 to your computer and use it in GitHub Desktop.
Save Ayc0/93caeb7195741a497d1e25a111e95325 to your computer and use it in GitHub Desktop.
Python's range
for (let x of range(10)) {
console.log(x);
}
for (let x of range(-10, 10)) {
console.log(x);
}
for (let x of range(10, -10, -1)) {
console.log(x);
}
function* range() {
let from = 0;
let to = 0;
let step = 1;
if (arguments.length == 1) {
to = arguments[0];
} else if (arguments.length >= 2) {
from = arguments[0];
to = arguments[1];
}
if (arguments.length >= 3) {
step = arguments[2];
}
if (step === 0) {
return
}
if (step > 0) {
while (from < to) {
yield from;
from += step;
}
} else {
while (from > to) {
yield from;
from += step;
}
}
}
module.exports = range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment