JavaScript Custom Iterables — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Custom Iterables 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
Make an object iterable
easy10 minAdd a Symbol.iterator generator to a range object.
- 2
Make a class iterable
easy10 minAdd [Symbol.iterator] to a class using generator method syntax.
- 3
Transform values while iterating
easy10 minYield an uppercased version of each item.
- 4
Write an iterable without a generator
easy10 minReturn a plain iterator object from Symbol.iterator.
- 5
Destructure a custom iterable
medium10 minPull two values out of a custom iterable object.
Find the bug(5)
- 1
Fix the missing brackets around Symbol.iterator
easy8 minSymbol.iterator is a computed key and needs square brackets.
- 2
Fix this lost inside the generator
easy8 minNeither an arrow function nor a plain function called without a receiver keeps this bound to the object.
- 3
Fix the class generator method missing its star
easy8 minWithout the star, this is not a generator and cannot yield.
- 4
Fix the manual iterator that never finishes
easy8 minWithout a done: true branch, the iterator keeps handing out results past the end of the range.
- 5
Fix the destructuring reading properties instead of iterating
easy8 minDestructuring a custom iterable uses its iterator, not its property names — which here are not even called from and to.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace