A short as possible FizzBuzz JS oneliner

After a few iterations this is what I came up with:


Array.apply(null, {length: 100}).map((_, x) => {x++; return x % 15 ? x % 5 ? x % 3 ? x : 'Fizz' : 'Buzz' : 'FizzBuzz'})

  • Initialize the array with 0-99 on the same line we use it. Map’s callback’s first argument is in this case always undefined so it’s not needed (the variable name is an underscore, if it doesn’t show), the second one is the element’s index.
Array.apply(null, {length: 100}).map((_, x)
  • x has to be incremented to stay true to the 1-100 FizzBuzz and for the truthyness trick below to work
x++;
  • By nesting the ternary operations like this we can skip the == 0 checks because > 0 remainders are truthy
x % 15 ? x % 5 ? x % 3 ? x : 'Fizz' : 'Buzz' : 'FizzBuzz'

It fits in a tweet!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s