Skip to content

Instantly share code, notes, and snippets.

@jschomay
Last active August 29, 2015 14:16
Show Gist options
  • Save jschomay/7f84b29817039fb3cea3 to your computer and use it in GitHub Desktop.
Save jschomay/7f84b29817039fb3cea3 to your computer and use it in GitHub Desktop.
Functional FizzBuzz
# My functional implementation of the common "fizzbuzz" interview coding test
FIZZBUZZMAP = [null, "fizz", "buzz", "fizzbuzz"]
isDivisibleBy = (d) ->
(n) -> !(n%d)
isFizz = isDivisibleBy 3
isBuzz = isDivisibleBy 5
doFizzBuzz = (n) ->
out = 0
if isFizz n then out += 1
if isBuzz n then out += 2
out
displayFizzBuzz = (n) ->
FIZZBUZZMAP[doFizzBuzz n] or n
console.log [1..16].map doFizzBuzz
console.log [1..16].map displayFizzBuzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment