Skip to main content

JavaScript Generator Functions — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Generator Functions 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

    Write a generator

    easy10 min

    Yield three numbers from a generator function.

  2. 2

    Use for of on a generator

    easy10 min

    Loop over a generator directly without calling next.

  3. 3

    Read the final return value

    easy10 min

    Report what the last, done result carries.

  4. 4

    Spread a generator

    easy10 min

    Spread a generator's yields into an array.

  5. 5

    Delegate with yield*

    medium10 min

    Hand off to another generator partway through.

Find the bug(5)

  1. 1

    Fix the missing star

    easy8 min

    Without the star, this is an ordinary function and yield is a syntax error.

  2. 2

    Fix the generator called like a normal function

    easy8 min

    Calling it returns an iterator immediately, without running the body yet.

  3. 3

    Fix yield used where yield* was needed

    easy8 min

    yield alone hands out the inner generator itself, not its values.

  4. 4

    Fix the return value read as a normal yield

    easy8 min

    The value from return only appears once, on the final done result.

  5. 5

    Fix the loop reading next() results directly

    easy8 min

    for of already unwraps the value; reading .value again is one step too many.

Ready to write the code?

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

Open the workspace