Skip to main content

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. 1

    Make an object iterable

    easy10 min

    Add a Symbol.iterator generator to a range object.

  2. 2

    Make a class iterable

    easy10 min

    Add [Symbol.iterator] to a class using generator method syntax.

  3. 3

    Transform values while iterating

    easy10 min

    Yield an uppercased version of each item.

  4. 4

    Write an iterable without a generator

    easy10 min

    Return a plain iterator object from Symbol.iterator.

  5. 5

    Destructure a custom iterable

    medium10 min

    Pull two values out of a custom iterable object.

Find the bug(5)

  1. 1

    Fix the missing brackets around Symbol.iterator

    easy8 min

    Symbol.iterator is a computed key and needs square brackets.

  2. 2

    Fix this lost inside the generator

    easy8 min

    Neither an arrow function nor a plain function called without a receiver keeps this bound to the object.

  3. 3

    Fix the class generator method missing its star

    easy8 min

    Without the star, this is not a generator and cannot yield.

  4. 4

    Fix the manual iterator that never finishes

    easy8 min

    Without a done: true branch, the iterator keeps handing out results past the end of the range.

  5. 5

    Fix the destructuring reading properties instead of iterating

    easy8 min

    Destructuring 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