Last active
August 29, 2015 14:16
-
-
Save jschomay/7f84b29817039fb3cea3 to your computer and use it in GitHub Desktop.
Functional FizzBuzz
This file contains hidden or 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
# 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