Recursion: Fibonacci Word

Published by Mubashir Hassan in

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.
  • You can check on the Resources tab for details about recursion.
  • A non-recursive version of this challenge can be found here.
Watch a quick demo on how Edabit works.