Created
March 11, 2021 12:53
Revisions
-
zlatkov created this gist
Mar 11, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ 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; } else if (skipCount > 0){ skipCount--; } let nextNumber = currentNumber + previousNumber; previousNumber = currentNumber; currentNumber = nextNumber; } } catch(err) { console.log(err.message); // will print ‘External throw’ on the fourth iteration. } } let fibonacciSequenceGenerator = makeFibonacciSequenceGenerator(50); console.log(fibonacciSequenceGenerator.next(1).value); console.log(fibonacciSequenceGenerator.next(3).value); console.log(fibonacciSequenceGenerator.next().value); fibonacciSequenceGenerator.throw(new Error('External throw')); console.log(fibonacciSequenceGenerator.next(1).value); // undefined will be printed since the generator is done.