Write a generator range(start, end, step) that yields numbers from start up to, but not including, end.
step defaults to 1 and may be negative to count down. Called with one argument, range(n) counts from 0 up to n. A step of 0 should throw a RangeError. It must also work when end is Infinity, producing numbers only as they're asked for.
function* range(start, end, step = 1) {
// your code
}
// [...range(5)]; // [0, 1, 2, 3, 4]
// [...range(2, 10, 3)]; // [2, 5, 8]
// [...range(5, 0, -2)]; // [5, 3, 1]
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…