Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. function* fibGenerator() {
  2. yield 0
  3. yield 1
  4.  
  5. let [ prev, curr ] = [ 0, 1 ]
  6. while (true) {
  7. [ prev, curr ] = [ curr, prev + curr ]
  8. yield curr
  9. }
  10. }
  11.  
  12. function fibIterator(n) {
  13. let count = 0
  14. let fibGen = fibGenerator()
  15. let result = []
  16.  
  17. for (fibNum of fibGen) {
  18. result.push(fibNum)
  19. count ++
  20.  
  21. if (count >= n) {
  22. break
  23. }
  24. }
  25. return result
  26. }
  27.  
  28. console.log(fibIterator(10))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement