Last active
September 28, 2016 10:21
-
-
Save juanmaguitar/d2784510feb41ae62d551a7746273b60 to your computer and use it in GitHub Desktop.
Iterators Fibonacci (ES2015)
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 fibonacci = { | |
[Symbol.iterator]() { | |
let pre = 0, cur = 1 | |
return { | |
next () { | |
[ pre, cur ] = [ cur, pre + cur ] | |
return { done: false, value: cur } | |
} | |
} | |
}, | |
getSerie(num) { | |
let arrFib = []; | |
for (let n of this) { | |
if (n > num) break | |
arrFib.push(n); | |
} | |
return arrFib.join(" - ") | |
} | |
} | |
> fibonacci.getSerie(100) === "1 - 2 - 3 - 5 - 8 - 13 - 21 - 34 - 55 - 89" // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment