Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fibCallback = (count = 0, callback) => {
- let f0 = 0, f1 = 1, f;
- for (let i = 0; i < count; i++) {
- f = f0 + f1;
- callback(f0 + ' + ' + f1 + ' = ' + f);
- f0 = f1;
- f1 = f;
- }
- }
- function* fibGenerator() {
- let f0 = 0, f1 = 1, f = 0;
- while(true) {
- f = f0 + f1;
- yield f0 + ' + ' + f1 + ' = ' + f;
- f0 = f1;
- f1 = f;
- }
- }
- const fibIterable = {
- count: Infinity,
- *[Symbol.iterator]() {
- let f0 = 0, f1 = 1, f = 0, i = 0;
- while(f < Infinity && i < count) {
- f = f0 + f1;
- yield f0 + ' + ' + f1 + ' = ' + f;
- f0 = f1;
- f1 = f;
- i++;
- }
- }
- }
- const count = 20;
- console.log('Fibonacci Series with arrow function with callback');
- fibCallback(count, s => console.log(s));
- console.log(''); // New Line
- console.log('Fibonacci Series with generator function and yield');
- const fibGen = fibGenerator();
- for (let i = 0; i < count; i++) {
- console.log(fibGen.next().value);
- }
- console.log(''); // New Line
- console.log('Fibonacci Series with iterable object and iterator');
- fibIterable.count = count;
- for (let value of fibIterable) {
- console.log(value);
- }
Add Comment
Please, Sign In to add comment