Files
30-seconds-of-code/snippets/range-generator.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

29 lines
677 B
Markdown

---
title: Range generator
tags: function,generator
author: chalarangelo
cover: dark-leaves-6
firstSeen: 2020-10-11T17:05:55+03:00
lastUpdated: 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`.
```js
const rangeGenerator = function* (start, end, step = 1) {
let i = start;
while (i < end) {
yield i;
i += step;
}
};
```
```js
for (let i of rangeGenerator(6, 10)) console.log(i);
// Logs 6, 7, 8, 9
```