Skip to content

Instantly share code, notes, and snippets.

@sleepyfox
Created December 26, 2011 15:33
Show Gist options
  • Save sleepyfox/1521426 to your computer and use it in GitHub Desktop.
Save sleepyfox/1521426 to your computer and use it in GitHub Desktop.
Fizz Buzz implementation in Coffeescript
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]
@sleepyfox
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment