Answers for "generator function fibonacci"

0

generator function fibonacci

// Fibonacci generator
function* fibonacci() {
  var a = 0;
  var b = 1;
  while (true) {
    yield a;
    a = b;
    b = a + b;
  }
}

// Instantiates the fibonacci generator
fib = fibonacci();

// gets first 10 numbers from the Fibonacci generator starting from 0
for (let i = 0; i < 10; i++) {
  console.log(i + ' => ' + fib.next().value);
}
Posted by: Guest on January-04-2021

Code answers related to "generator function fibonacci"

Code answers related to "Javascript"

Browse Popular Code Answers by Language