generator function fibonacci

Solutions on MaxInterview for generator function fibonacci by the best coders in the world

showing results for - "generator function fibonacci"
Emmanuel
24 Jan 2020
1// Fibonacci generator
2function* fibonacci() {
3  var a = 0;
4  var b = 1;
5  while (true) {
6    yield a;
7    a = b;
8    b = a + b;
9  }
10}
11
12// Instantiates the fibonacci generator
13fib = fibonacci();
14
15// gets first 10 numbers from the Fibonacci generator starting from 0
16for (let i = 0; i < 10; i++) {
17  console.log(i + ' => ' + fib.next().value);
18}