Send a name into a paused generator.
Acceptance criteria
- 1.#out should show Hello Ada.
JavaScript Tutorial · JavaScript Generators & Iterators
These 10 tasks go with the JavaScript Generator Communication chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Values can be sent into a running generator through next, not just read out of it.” .
1.Send a value inexercise · easy · 10 min
Send a name into a paused generator.
Passes when: #out should show Hello Ada.
2.Show the first next only starts itexercise · easy · 10 min · practice plan
Confirm the value passed to the first call is ignored.
Passes when: #out should show not used.
3.Build a running totalexercise · easy · 10 min · practice plan
Add several values into a generator one at a time.
Passes when: #out should show 15.
4.Stop a generator earlyexercise · easy · 10 min · practice plan
Call return on a generator and check it reports done.
Passes when: #out should show true.
5.Throw into a generatorexercise · medium · 10 min · practice plan
Throw an error into a generator that catches it.
Passes when: #out should show caught: stop.
6.Fix the value expected on the first callbug fix · easy · 8 min
Nothing is paused yet when the first next() runs, so its argument is thrown away.
Passes when: #out should show undefined.
7.Fix the total never sent inbug fix · easy · 8 min · practice plan
Calling next with no argument never advances the running total.
Passes when: #out should show 5.
8.Fix the generator used after returnbug fix · easy · 8 min · practice plan
Once returned, further calls should stay done.
Passes when: #out should show true.
9.Fix the throw with no catch insidebug fix · easy · 8 min · practice plan
Without a try block inside, the thrown error escapes the generator entirely instead of being handled.
Passes when: #out should show caught inside.
10.Fix while true assumed to hangbug fix · easy · 8 min · practice plan
A while true loop inside a generator is safe, since yield pauses it — capping it out of caution breaks the sequence early.
Passes when: #out should show 0,1,2.
Between them these tasks cover next(value), return() and throw().
Looking for a different chapter? See every practice set, or switch to the interview theory questions.