Monthly Archives: September 2015

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!