Created
December 26, 2011 15:33
-
-
Save sleepyfox/1521426 to your computer and use it in GitHub Desktop.
Fizz Buzz implementation in Coffeescript
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
rules = [ | |
{ divisor: 3, word: "fizz" } | |
{ divisor: 5, word: "buzz" } | |
] | |
fizzBuzz = (n) -> | |
say = "" | |
say += i.word for i in rules when n % i.divisor is 0 | |
say || n | |
console.log fizzBuzz i for i in [1..100] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Although this can be written idiomatically as the one-liner:
console.log i for i in [1..100].map (n) -> s = (s || "") + w for [d,w] in [[3,"fizz"],[5,"buzz"]] when n%d is 0; s || n
The multi-line code is far more readable, and thus preferable if you are not trying to communicate over twitter.