// Generator which creates a range of numbers
function* range( start, end ) {
  let i = start;
  while (i < end) {
    yield i++;
  }
}

// The previous generator function is functionally equivalent to the next one.
// A function that takes 2 parameters and returns an object with a next method
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol
const range2 = (start, end) => ({
  i: start,
  next() {
        while (this.i < end) {
          return { value: this.i++, done: false };
        }
        return { value: undefined, done: true };
      },
  [Symbol.iterator]() {
    return this;
  },
});

for (let n of range2(0, 20)) {
  console.log(n); // 0...19
}

const range0to2 = range2(0,2);
console.log(range0to2.next().value) // 0
console.log(range0to2.next().value) // 1
console.log(range0to2.next().value) // undefined

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.