Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Fibonacci Generator

Beyond the Coursemedium

Implement `fibSeq(n)` using a generator that yields the first `n` Fibonacci numbers starting `0, 1`, collected into an array.

Sample tests

Input: fibSeq(5)
Output: [0,1,1,2,3]
Input: fibSeq(1)
Output: [0]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Yielding after advancing skips the first value.

Learning resources

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

The generator yields the current term then advances the pair, producing the first n Fibonacci numbers.

Loading...
⌘/Ctrl + Enter

Run your code to see results.