Last active
June 11, 2019 20:03
-
-
Save coryf/b00b9c4c54a465c76a1fae75eb6f5cb4 to your computer and use it in GitHub Desktop.
javascript fizzbuzz functional iterator
This file contains 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
const fizzBuzz = { | |
[Symbol.iterator]: (() => { | |
const stop = 100 | |
const iterate = function* (number = 1, count5 = 1, count7 = 1) { | |
if (number > stop) { return } | |
if (count5 == 5 && count7 == 7) { | |
yield 'fizzbuzz' | |
yield *iterate(number + 1, 1, 1) | |
} | |
else if (count5 == 5) { | |
yield 'fizz' | |
yield *iterate(number + 1, 1, count7 + 1) | |
} | |
else if (count7 == 7) { | |
yield 'buzz' | |
yield *iterate(number + 1, count5 + 1, 1) | |
} | |
else { | |
yield number | |
yield *iterate(number + 1, count5 + 1, count7 + 1) | |
} | |
} | |
return iterate | |
})() | |
} | |
console.log([...fizzBuzz]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment