Skip to main content

JavaScript Iterators — exercises and bug fixes

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

    Call next manually

    easy10 min

    Get an array's iterator and read its first value.

  2. 2

    Check the done flag

    easy10 min

    Drain a one-element array and check the second call is done.

  3. 3

    Write a custom iterator

    easy10 min

    Build an iterator object that counts to three.

  4. 4

    Drain an iterator by hand

    easy10 min

    Collect every value from a custom iterator into an array.

  5. 5

    Check for the iterator method

    medium10 min

    Report whether a plain object has Symbol.iterator.

Find the bug(5)

  1. 1

    Fix the value read as the whole result

    easy8 min

    next() returns an object; the value is one property of it.

  2. 2

    Fix the loop that never stops

    easy8 min

    Without checking done, the loop keeps calling next past the end.

  3. 3

    Fix the custom iterator returning a value with done true

    easy8 min

    Once finished, value should be undefined, not a leftover number.

  4. 4

    Fix the plain object assumed to be iterable

    easy8 min

    A plain object has no Symbol.iterator method, so spreading it throws.

  5. 5

    Fix done checked with the wrong value

    easy8 min

    done is a boolean, not the string "true".

Ready to write the code?

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

Open the workspace