Skip to main content

JavaScript Generator Communication — exercises and bug fixes

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

    Send a value in

    easy10 min

    Send a name into a paused generator.

  2. 2

    Show the first next only starts it

    easy10 min

    Confirm the value passed to the first call is ignored.

  3. 3

    Build a running total

    easy10 min

    Add several values into a generator one at a time.

  4. 4

    Stop a generator early

    easy10 min

    Call return on a generator and check it reports done.

  5. 5

    Throw into a generator

    medium10 min

    Throw an error into a generator that catches it.

Find the bug(5)

  1. 1

    Fix the value expected on the first call

    easy8 min

    Nothing is paused yet when the first next() runs, so its argument is thrown away.

  2. 2

    Fix the total never sent in

    easy8 min

    Calling next with no argument never advances the running total.

  3. 3

    Fix the generator used after return

    easy8 min

    Once returned, further calls should stay done.

  4. 4

    Fix the throw with no catch inside

    easy8 min

    Without a try block inside, the thrown error escapes the generator entirely instead of being handled.

  5. 5

    Fix while true assumed to hang

    easy8 min

    A while true loop inside a generator is safe, since yield pauses it — capping it out of caution breaks the sequence early.

Ready to write the code?

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

Open the workspace