Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #generator #function #fibonacci
ADD COMMENT
Topic
Name
9+8 =