Range generator in JavaScript
A generator that yields numbers lazily, so an enormous range costs nothing.
function* range(start, stop, step = 1) {
if (stop === undefined) {
stop = start;
start = 0;
}
for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
yield i;
}
}
Keywords and builtins used here
forfunctionifletyield
The run, in numbers
- Lines
- 9
- Characters to type
- 171
- Tokens
- 58
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 41 seconds.
Step 3 of 3 in Control flow, step 13 of 43 in Language basics.