Exercise 2: Fibonacci Series Index
Practice generator functions by implementing a Fibonacci series function.
We'll cover the following
Problem statement
In the previous exercise, we terminated the iteration when the value in the series exceeded 25. Let’s modify the fibonacciSeries()
function so that we can terminate the series when a certain number of values has been obtained. For example, the function will take a number, limit
as input and return an array containing total terms equivalent to the limit
value.
Function description
The function will yield Fibonacci numbers until the index of the Fibonacci sequence is less than the limit
. Since the index starts at zero, the function will yield limit + 1
numbers of the Fibonacci sequence. This is shown in the sample input and output table below.
Sample input and output
Input | Output |
---|---|
2 | 0,1,1 |
4 | 0,1,1,2,3 |
8 | 0,1, 1, 2, 3, 5, 8, 13, 21 |
12 | 0,1,1,2,3,5,8,13,21,34,55,89,144 |
Exercise
The solution to this exercise is available in the code widget below. However, it’ll be good practice to solve this problem on your own first. Good luck!
Get hands-on with 1400+ tech skills courses.