567 B
567 B
title, tags
| title | tags |
|---|---|
| rangeGenerator | function,generator,advanced |
Creates a generator, that generates all values in the given range using the given step.
- Use a
whileloop to iterate fromstarttoend, usingyieldto return each value and then incrementing bystep. - Omit the third argument,
step, to use a default value of1.
const rangeGenerator = function* (start, end, step = 1) {
let i = start;
while (i < end) {
yield i;
i += step;
}
};
for (let i of rangeGenerator(6, 10)) console.log(i);
// Logs 6, 7, 8, 9