Skip to main content

JavaScript Lazy Sequences — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Lazy Sequences lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Take from an infinite generator

    easy10 min

    Use a take helper to get the first five natural numbers.

  2. 2

    Cycle through a list forever

    easy10 min

    Take seven values from a repeating three-item cycle.

  3. 3

    Stop safely with break

    easy10 min

    Break out of a for of loop over an infinite generator after one value.

  4. 4

    Filter a lazy sequence

    easy10 min

    Take the first four even natural numbers.

  5. 5

    Confirm nothing is precomputed

    medium10 min

    Report that only one call to next has happened so far.

Find the bug(5)

  1. 1

    Fix the take helper reading the wrong property

    easy8 min

    The value is on .value, not the result object itself.

  2. 2

    Fix the cycle indexed without wrapping

    easy8 min

    Without the modulo, the index runs past the end of the array.

  3. 3

    Fix the loop with no break

    easy8 min

    Capping the generator itself is the wrong fix — break out of the loop instead, so the sequence stays infinite and reusable.

  4. 4

    Fix the filter yielding every value

    easy8 min

    The condition to keep only even numbers was never applied.

  5. 5

    Fix the array built ahead of time

    easy8 min

    Precomputing every value defeats the point of laziness — count how many are actually produced.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace