← Back to challenges

Recursion: Fibonacci Word

JavaScriptHardarraysrecursionstrings

Instructions

A Fibonacci word is a specific sequence of binary digits (or symbols from any two-letter alphabet). The Fibonacci word is formed via repeated concatenation in the same fashion that the Fibonacci numbers are formed via repeated addition.

Create a function that takes a number n as an argument and returns the first n elements of the Fibonacci word sequence.

Examples

generateWord(1) ➞ "invalid"
// if n < 2 then return "invalid".

generateWord(3) ➞ "b, a, ab"

generateWord(7) ➞ "b, a, ab, aba, abaab, abaababa, abaababaabaab"

Notes

  • You are expected to solve this challenge via recursion.

  • A non-recursive version of this challenge can be found here.

  • If you think recursion is fun, you can find a collection of such challenges here.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.