Solution Review: Fibonacci Series
Learn how to implement the generator function of the Fibonacci series.
We'll cover the following...
We'll cover the following...
Solution
The solution to the “Fibonacci series” challenge is given below.
Press + to interact
Node.js
const fibonocciSeries = function*(n) {let current = 0;let next = 1;yield* [current, next];while(true) {const temp = current;current = next;next = next + temp;if(next > n) break;yield next;}}for(const value of fibonocciSeries(25)) {console.log(value);}
Explanation
We need to implement a generator function that will yield the Fibonacci numbers.
- In the Fibonacci series, every subsequent number is the sum of two previous numbers. So, first, we need to set two starting points, which are