This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function* makeFibonacciSequenceGenerator(endIndex = Infinity) { | |
let previousNumber = 0; | |
let currentNumber = 1; | |
for (let currentIndex = 0; currentIndex < endIndex; currentIndex++) { | |
await new Promise(resolve => setTimeout(resolve, 1000)); // a simple timeout as an example. | |
yield currentNumber; | |
let nextNumber = currentNumber + previousNumber; | |
previousNumber = currentNumber; | |
currentNumber = nextNumber; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fibonacciSequenceGenerator = makeFibonacciSequenceGenerator(50); | |
console.log(fibonacciSequenceGenerator.next().value); // 1 | |
console.log(fibonacciSequenceGenerator.next(3).value); // 5 | |
console.log(fibonacciSequenceGenerator.next().value); // 8 | |
console.log(fibonacciSequenceGenerator.return(374).value); // 374 | |
console.log(fibonacciSequenceGenerator.next(1).value); // undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* makeFibonacciSequenceGenerator(endIndex = Infinity) { | |
let previousNumber = 0; | |
let currentNumber = 1; | |
let skipCount = 0; | |
try { | |
for (let currentIndex = 0; currentIndex < endIndex; currentIndex++) { | |
if (skipCount === 0) { | |
skipCount = yield currentNumber; | |
skipCount = skipCount === undefined ? 0 : skipCount; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* makeFibonacciSequenceGenerator(endIndex = Infinity) { | |
let previousNumber = 0; | |
let currentNumber = 1; | |
let skipCount = 0; | |
for (let currentIndex = 0; currentIndex < endIndex; currentIndex++) { | |
if (skipCount === 0) { | |
skipCount = yield currentNumber; // skipCount is the parameter passed through the invocation of `fibonacciSequenceGenerator.next(value)` below. | |
skipCount = skipCount === undefined ? 0 : skipCount; // makes sure that there is an input | |
} else if (skipCount > 0){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* makeFibonacciSequenceGenerator(endIndex = Infinity) { | |
let previousNumber = 0; | |
let currentNumber = 1; | |
for (let currentIndex = 0; currentIndex < endIndex; currentIndex++) { | |
yield currentNumber; | |
let nextNumber = currentNumber + previousNumber; | |
previousNumber = currentNumber; | |
currentNumber = nextNumber; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fibonacciSequenceIterator = makeFibonacciSequenceIterator(5); | |
for (let x of fibonacciSequenceIterator) { | |
console.log(x); //1 1 2 3 5 8 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeFibonacciSequenceIterator(endIndex = Infinity) { | |
let currentIndex = 0; | |
let previousNumber = 0; | |
let currentNumber = 1; | |
let iterator = {}; | |
iterator[Symbol.iterator] = () => { | |
return { | |
next: () => { | |
if (currentIndex >= endIndex) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fibonacciSequenceIterator = makeFibonacciSequenceIterator(5); | |
for (let x of fibonacciSequenceIterator) { | |
console.log(x); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fibonacciSequenceIterator = makeFibonacciSequenceIterator(5); // Generates the first 5 numbers. | |
let result = fibonacciSequenceIterator.next(); | |
while (!result.done) { | |
console.log(result.value); // 1 1 2 3 5 8 | |
result = fibonacciSequenceIterator.next(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeFibonacciSequenceIterator(endIndex = Infinity) { | |
let currentIndex = 0; | |
let previousNumber = 0; | |
let currentNumber = 1; | |
return { | |
next: () => { | |
if (currentIndex >= endIndex) { | |
return { value: currentNumber, done: true }; | |
} |
NewerOlder