Fizz Buzz golfing

# Fizz Buzz golfing

A colleague of mine came to me recently and gave me the following link, telling me to try the single challenge and make it as small as possible. It contained the classic Fizz Buzz problem.

I whipped a rather standard JavaScript solution, which passed the site's verification with flying colors. It wasn't exactly code golfing, doing things in a more "best-practice" way than a true "code golfing" manner: using a ternary expression, declaring the strings "Fizz" and "Buzz" as constants, instead of repeating them everytime and so on.

``````function fizzbuzz(n){
var ft='Fizz',
bt='Buzz',
f=(n%3==0),
b=(n%5==0);
return (f && b )? ft + bt : f ? ft : b ? b t :n;
}
``````

I showed it to him and he told me it was still too big. To me it seemed like I've done everything that was sanely possible. Sure, maybe I could get rid of the `var` declaration and make everything global, but that didn't seem fair.

"Everything goes", he said to me. Everything goes? Challenge accepted!

First thing I did was to get rid of those pesky "var" type declarations and use one-letter names for the variables. Then I removed the semi-colons and the paranthesis.

``````function fizzbuzz(n){
x='Fizz'
y='Buzz'
f=n%3==0
b=n%5==0
return f&&b?x+y:f?x:b?y:n
}
``````

77 characters (without whitespaces) said charcounter. I had to go deeper!

FizzBuzz makes you verify if a number is divisible by 3, 5 or both these values. Usually you do this by making sure the rest of the division is 0: `n%3==0`. If it's more than 0, it means it's not divisible. Does this mean you `n%3==0` is the same thing as `n%3<1` for integers? You bet it is, and it saves you a character. This passed the site's verification as well

``````function fizzbuzz(n){
x='Fizz'
y='Buzz'
f=n%3<1
b=n%5<1
return f&&b?x+y:f?x:b?y:n
}
``````

By now, I reached 75 characters without whitespace. Still, it wasn't small enough. My colleague's solution, seen below, had 73 characters!

``````function fizzbuzz (n) {
a="Fizz"
b="Buzz"
return n%3 == 0 ? n%5 == 0 ? a+b : a : n%5 == 0 ? b : n
}
``````

I had to beat him but I saw no way my current solution could be improved. But then... what about a different solution?
What if I were to take a completely different approach.

We usually checked if n%3 was 0, which meant we needed some extra-characters, either `<1` or `==0`. But what if we went the other way around and checked if n%3 was not 0? This would involve only checking the `n%3` expression, to see if it evaluates to true or false.

In a saner, classic way, it would look like this:

``````if (n%3 !== 0) {
if (n%5 !== 0) {
return n
} else {
return 'Buzz'
}
} else {
if (n%5 !== 0) {
return 'Fizz'
} else {
return 'FizzBuzz'
}
}
``````

By converting this to a cryptical looking thernary, I got a whooping 64 characters without white space! Woohoo!

``````function fizzbuzz(n){
f='Fizz'
b='Buzz'
return n%3 ? n%5 ? n : b : n%5 ? f : f+b
}
``````

It was a fun challenge, in particular the competition part. It forces you to reach the limits of the programming language and makes sure you also know the gotchas too, not just the mainstream stuff.

Yeah, I should do this more often!