Skip to content
Foxipa
Code Completion 12 Challenges

Complete the JavaScript Iterators and Generators Code

Practice the iterable protocol, generator control flow, delegation, and lazy iterator helpers.

Intermediate ~8 min read

Challenge 1

Create the iterator returned by Symbol.iterator.

javascript Your answer
const iterable = {
  values: [10, 20],
  [Symbol.iterator]() {
    return ;
  }
};

Press Ctrl⌘Enter to check your answer

Challenge 2

Turn the function into a generator that yields two values.

javascript Your answer
function* values() {
  
  yield 2;
}

Press Ctrl⌘Enter to check your answer

Challenge 3

Resume the generator with a value for its paused yield.

javascript Your answer
function* ask() {
  const answer = yield "question";
  return answer;
}
const it = ask();
it.next();
const result = it.;

Press Ctrl⌘Enter to check your answer

Challenge 4

Delegate to an existing iterable.

javascript Your answer
function* values() {
  yield "start";
   [1, 2];
}

Press Ctrl⌘Enter to check your answer

Challenge 5

Limit a modern iterator pipeline to two values.

javascript Your answer
const result = Iterator.from([1, 2, 3]).(2).toArray();

Press Ctrl⌘Enter to check your answer

Challenge 6

Transform values lazily with an iterator helper.

javascript Your answer
const result = Iterator.from([1, 2, 3]).(value => value * 2).toArray();

Press Ctrl⌘Enter to check your answer

Challenge 7

Keep only values matching a predicate.

javascript Your answer
const result = Iterator.from([1, 2, 3, 4]).(value => value % 2 === 0).toArray();

Press Ctrl⌘Enter to check your answer

Challenge 8

Adapt an iterable to the Iterator API.

javascript Your answer
const iterator = (new Set([1, 2, 3]));

Press Ctrl⌘Enter to check your answer

Challenge 9

Combine two iterables and consume the zipped iterator.

javascript Your answer
const pairs = Iterator.zip([[1, 2], ["a", "b"]]);
const result = pairs.;

Press Ctrl⌘Enter to check your answer

Challenge 10

Require the two iterables to have the same length.

javascript Your answer
const result = Iterator.zip([[1, 2], ["a", "b"]], { mode:  }).toArray();

Press Ctrl⌘Enter to check your answer

Challenge 11

Create keyed zipped rows from names and scores.

javascript Your answer
const rows = Iterator.zipKeyed({ name: ["Ada"], score: [10] }).;

Press Ctrl⌘Enter to check your answer

Challenge 12

Close an iterator explicitly through the iterator protocol.

javascript Your answer
const iterator = [1, 2][Symbol.iterator]();
iterator.next();
iterator.;

Press Ctrl⌘Enter to check your answer