Command Palette

Search for a command to run...

Hectal
Phase 14: Advanced Language
JavaScript · Exercise 2 of 5 PreviousNext
Intermediate

A lazy range() generator

Write a generator function range(start, end, step = 1) that yields start, start + step, ... up to but not including end. Assume step is positive.

It must be lazy: range(0, Infinity) must work and hand out numbers one at a time.

Use it to fill #evens with the even numbers from 0 up to (not including) 20, written as 0, 2, 4, ..., 18.

What Check tests

  1. range(1, 5) gives 1, 2, 3, 4
  2. range(0, 10, 3) gives 0, 3, 6, 9
  3. range(5, 5) gives nothing
  4. It's lazy: range(0, Infinity) hands out 0, then 1
  5. It's written as a generator (function*)
  6. The page shows 0, 2, 4, 6, 8, 10, 12, 14, 16, 18

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.