Files
30-seconds-of-code/snippets/rangeGenerator.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

677 B

title, tags, author, cover, firstSeen, lastUpdated
title tags author cover firstSeen lastUpdated
Range generator function,generator chalarangelo dark-leaves-6 2020-10-11T17:05:55+03:00 2020-10-11T17:05:55+03:00

Creates a generator, that generates all values in the given range using the given step.

  • Use a while loop to iterate from start to end, using yield to return each value and then incrementing by step.
  • Omit the third argument, step, to use a default value of 1.
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