Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Last active September 28, 2016 10:21
Show Gist options
  • Save juanmaguitar/d2784510feb41ae62d551a7746273b60 to your computer and use it in GitHub Desktop.
Save juanmaguitar/d2784510feb41ae62d551a7746273b60 to your computer and use it in GitHub Desktop.
Iterators Fibonacci (ES2015)
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