Skip to curriculum
Jump to the editor

10.1. next() over an array, one value per call

Easy
Topics
10–20 min

for...of, spread ([...list]) and Array.from all read an array the same way: they ask it for an iterator, then call the iterator's next() again and again. Each call hands out one item, wrapped as { value, done: false }, until a call answers { value: undefined, done: true }. Writing that object yourself is the clearest way to see what every loop over an array is doing underneath, and it is the shape you need whenever you make your own data iterable.

Write createIterator(array) in iterator.js. It returns an iterator that walks array from the first item to the last, one item per next() call, the way the built-in array iterator does.

Signature

JAVASCRIPT
function createIterator(array) // returns an iterator object
  • array: an array, or any array-like object (an object with a length and items at 0, 1, …).
  • Returns an object with a next() method and a [Symbol.iterator]() method.
  • next() returns a new object { value, done } on every call.

Requirements

  1. Each next() returns the next item as { value: item, done: false }, in index order, starting at index 0.
  2. After the last item, next() returns exactly { value: undefined, done: true }, with both keys.
  3. Once it has returned done: true, it keeps returning { value: undefined, done: true } on every later call, even if items are added to the array afterwards.
  4. Items that are undefined, null, 0, "" or false, and holes in a sparse array, still count as items: done is false for them (a hole's value is undefined).
  5. The iterator reads array.length on every call, so an item pushed before the end is handed out too.
  6. The array is never changed: no item is removed or moved.
  7. Each call to createIterator has its own position; two iterators over the same array do not move each other.
  8. Every next() returns a fresh object, never the same object changed.
  9. The iterator is iterable: [Symbol.iterator]() returns the iterator itself, so for...of and spread work on it.
  10. It works on array-like objects too.
  11. It is a plain object with a next method, not a generator, and it does not use the built-in array iterator.

Examples

JAVASCRIPT
const it = createIterator(["a", "b"]);
it.next(); // { value: "a", done: false }
it.next(); // { value: "b", done: false }
it.next(); // { value: undefined, done: true }
it.next(); // { value: undefined, done: true }
JAVASCRIPT
const holes = createIterator([1, , 3]);
holes.next(); // { value: 1, done: false }
holes.next(); // { value: undefined, done: false }: a hole is still an item
holes.next(); // { value: 3, done: false }
JAVASCRIPT
const list = [1, 2];
const it = createIterator(list);
it.next();           // { value: 1, done: false }
list.push(3);
[...it];             // [2, 3]: spread finishes what next() started
createIterator({ length: 2, 0: "x", 1: "y" }).next(); // { value: "x", done: false }

Environment

  • Plain JavaScript in a browser page; the tests call the global createIterator from iterator.js.
  • Do not use a generator function (function*) or the array's own iterator (array[Symbol.iterator](), values(), keys(), entries()). The tests replace the built-in array iterator with one that throws while they run.
  • A return() method (closing the iterator early) is not part of this problem.

Hints

  1. Which two pieces of state must survive from one next() call to the next?
  2. Requirement 3 says "keeps returning done", even after a push. Is checking index < array.length alone enough for that?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…