cf.completefrontend

Write the codeHard

Write a Range class whose instances work with for...of and spread, without using a generator.

new Range(from, to) covers the integers from from to to, both included. A range must be reusable: you can spread it twice, or loop over it inside another loop over the same range. The iterator you return should itself be iterable, like the built-in ones.

Write this
class Range {
  constructor(from, to) {
    // your code
  }
 
  [Symbol.iterator]() {
    // your code
  }
}
 
// [...new Range(1, 4)]; // [1, 2, 3, 4]
// for (const n of new Range(1, 3)) console.log(n); // 1, 2, 3

Solve it in the editor, with tests

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…