OscarRobertRodriguez
9/10/2017 - 8:45 PM

JS Bin // source https://jsbin.com/luxexus

JS Bin

// source https://jsbin.com/luxexus

// init the generator function
function* fibonacci(counter) {
  // here we initialize our variables , counter, and our array
  let fn1 = 0;
  let fn2 = 1;
  let nums = [];
  // we use a while loop for our return 
  while (counter > 0) {
    // current takes the fn1 value which starts at 0
    let current = fn1;
    // fn1 takes the fn2 value which starts at 1
    fn1 = fn2;
    // fn2 takes on the cureent and fn1 which first run would 
    // be 1
    fn2 = current + fn1;
    // we subtract from our counter
    counter--; 
    // we push our current value to the array
    nums.push(current);
  }
   yield* nums; 
};

// we pass a parameter of 10 to get the first 10 numbers of the fibonacci sequence
// but we could put any number for our desired return
let myFibonacciGenerator = fibonacci(10); 

// console.log using the next iterator
// we have eleven consoles to illustrate what happens when we get to the end 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value);
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value);
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next());
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

<script id="jsbin-javascript">
// init the generator function
function* fibonacci(counter) {
  // here we initialize our variables , counter, and our array
  let fn1 = 0;
  let fn2 = 1;
  let nums = [];
  // we use a while loop for our return 
  while (counter > 0) {
    // current takes the fn1 value which starts at 0
    let current = fn1;
    // fn1 takes the fn2 value which starts at 1
    fn1 = fn2;
    // fn2 takes on the cureent and fn1 which first run would 
    // be 1
    fn2 = current + fn1;
    // we subtract from our counter
    counter--; 
    // we push our current value to the array
    nums.push(current);
  }
   yield* nums; 
};

// we pass a parameter of 10 to get the first 10 numbers of the fibonacci sequence
// but we could put any number for our desired return
let myFibonacciGenerator = fibonacci(10); 

// console.log using the next iterator
// we have eleven consoles to illustrate what happens when we get to the end 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value);
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value);
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next());
</script>



<script id="jsbin-source-javascript" type="text/javascript">// init the generator function
function* fibonacci(counter) {
  // here we initialize our variables , counter, and our array
  let fn1 = 0;
  let fn2 = 1;
  let nums = [];
  // we use a while loop for our return 
  while (counter > 0) {
    // current takes the fn1 value which starts at 0
    let current = fn1;
    // fn1 takes the fn2 value which starts at 1
    fn1 = fn2;
    // fn2 takes on the cureent and fn1 which first run would 
    // be 1
    fn2 = current + fn1;
    // we subtract from our counter
    counter--; 
    // we push our current value to the array
    nums.push(current);
  }
   yield* nums; 
};

// we pass a parameter of 10 to get the first 10 numbers of the fibonacci sequence
// but we could put any number for our desired return
let myFibonacciGenerator = fibonacci(10); 

// console.log using the next iterator
// we have eleven consoles to illustrate what happens when we get to the end 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value);
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value);
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next().value); 
console.log(myFibonacciGenerator.next()); 
</script></body>
</html>