Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Take Evens from an Infinite Generator

Beyond the Coursemedium

Implement `takeEvens(n)` that pulls the first `n` even numbers from an infinite generator (`0, 2, 4, ...`), returning them as an array.

Sample tests

Input: takeEvens(3)
Output: [0,2,4]
Input: takeEvens(1)
Output: [0]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Spreading an infinite generator never terminates; take only n.

Learning resources

  • MDN: Iterators and generators
Visualize this concept: Iterators and Generators →
Approach & explanation (try first)

An unbounded generator paired with a bounded take loop yields the first n even numbers without hanging.

Loading...
⌘/Ctrl + Enter

Run your code to see results.