Guest User

Untitled

a guest
Dec 13th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. const fibCallback = (count = 0, callback) => {
  2. let f0 = 0, f1 = 1, f;
  3.  
  4. for (let i = 0; i < count; i++) {
  5. f = f0 + f1;
  6. callback(f0 + ' + ' + f1 + ' = ' + f);
  7. f0 = f1;
  8. f1 = f;
  9. }
  10. }
  11.  
  12. function* fibGenerator() {
  13. let f0 = 0, f1 = 1, f = 0;
  14.  
  15. while(true) {
  16. f = f0 + f1;
  17. yield f0 + ' + ' + f1 + ' = ' + f;
  18. f0 = f1;
  19. f1 = f;
  20. }
  21. }
  22.  
  23. const fibIterable = {
  24. count: Infinity,
  25.  
  26. *[Symbol.iterator]() {
  27. let f0 = 0, f1 = 1, f = 0, i = 0;
  28.  
  29. while(f < Infinity && i < count) {
  30. f = f0 + f1;
  31. yield f0 + ' + ' + f1 + ' = ' + f;
  32. f0 = f1;
  33. f1 = f;
  34. i++;
  35. }
  36. }
  37. }
  38.  
  39. const count = 20;
  40.  
  41. console.log('Fibonacci Series with arrow function with callback');
  42. fibCallback(count, s => console.log(s));
  43. console.log(''); // New Line
  44.  
  45. console.log('Fibonacci Series with generator function and yield');
  46. const fibGen = fibGenerator();
  47. for (let i = 0; i < count; i++) {
  48. console.log(fibGen.next().value);
  49. }
  50. console.log(''); // New Line
  51.  
  52. console.log('Fibonacci Series with iterable object and iterator');
  53. fibIterable.count = count;
  54. for (let value of fibIterable) {
  55. console.log(value);
  56. }
Add Comment
Please, Sign In to add comment