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
Write a generator
easy10 minYield three numbers from a generator function.
- 2
Use for of on a generator
easy10 minLoop over a generator directly without calling next.
- 3
Read the final return value
easy10 minReport what the last, done result carries.
- 4
Spread a generator
easy10 minSpread a generator's yields into an array.
- 5
Delegate with yield*
medium10 minHand off to another generator partway through.
Find the bug(5)
- 1
Fix the missing star
easy8 minWithout the star, this is an ordinary function and yield is a syntax error.
- 2
Fix the generator called like a normal function
easy8 minCalling it returns an iterator immediately, without running the body yet.
- 3
Fix yield used where yield* was needed
easy8 minyield alone hands out the inner generator itself, not its values.
- 4
Fix the return value read as a normal yield
easy8 minThe value from return only appears once, on the final done result.
- 5
Fix the loop reading next() results directly
easy8 minfor 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