JavaScript FizzBuzz in a tweet

The FizzBuzz challenge has been around a while but I stumbled across it again after reading another unique Giles Bowkett post.

If you’re not familiar with FizzBuzz, it’s a little ‘challenge’ designed to test a candidate programmer’s ability to perform a simple task. In this case, you just have to print out the numbers from 1 to 100, unless the number is a multiple of 3, when you should instead print “Fizz”, 5 in which case you print “Buzz”, or both 3 and 5 in which case you print “FizzBuzz”.

Here’s a trivial JavaScript implementation:

for (var i=1; i <= 100; i++) {
  if (i % 3 == 0) {
    if (i % 5 == 0) {
    } else {
  } else if (i % 5 == 0) {
  } else {

Pretty simple stuff, but a bit verbose. I wanted something that would fit into a tweet. It turns out that’s pretty simple – this is 133 characters including whitespace, 7 within tolerance for a twitter message:

for (var i = 1; i <= 100; i++) {
  var f = i % 3 == 0, b = i % 5 == 0;
  console.log(f ? b ? "FizzBuzz" : "Fizz" : b ? "Buzz" : i);

Which of course begs the question – just how short can a JavaScript FizzBuzz implementation be? Here’s my baseline, which is a tortured and contorted version of the above:


The above is 71 characters – I expect you to do better. The rules are that the only dependency is Firebug’s console.log being available, and you can’t replace ‘console.log’ for anything else.

Of course, if we did swap ‘console.log’ for ‘alert’, the whole thing would fit in a tweet twice, but that would be damn annoying.

Hint: you can take at least three more characters off the above – can you see how?

19 Responses to JavaScript FizzBuzz in a tweet

  1. Lloyd K says:


    Stolen from Evan on IRC 😉

  2. Joshua F says:


    67 chars, but it’s getting ugly. No bounding braces, no var, no trailing semi-colon…

  3. Lloyd K says:

    Hmm, seems you need to declare i first. Epic fail!

  4. Joshua F says:

    that’s an initial decrement operator ( – – i ) in the while comparison clause, btw

  5. Lloyd K says:


    Again stolen from Evan…he needs to get on the ball 😉

  6. Lloyd K says:


    Stolen from Evan yet again 😛

  7. JayTee says:

    I only changed the for loop – though I like the Lloyd K solution (stolen from Evan) better than what I came up with

    I only reduced by 1 character 😦


  8. LJHarb says:

    I’m really not that impressed with a solution that doesn’t pass JSLint. Are we programmers or teenagers texting at the mall?

  9. Ed Spencer says:

    @Joshua – nice, though as you point your solution counts down from 100, not up 🙂

    @Lloyd (though really @Evan) – this looks great, 64 characters and the current leader

    @LJHarb – jslint is very close to my heart and the 133 char version does pass. After that though the challenge is about how few characters you can use to execute the result – it’s for fun, not a real world problem.

  10. Teresa says:

    Stumbled across this on Twitter — fun. This solution is 65 (obviously not the winner), and at least runs fine in the Firebug console:


  11. Ricardo says:

    It says nothing about the order 😀


  12. Ed Spencer says:

    @Teresa: much better than mine and just 1 behind current leader Evan

    @Ricardo I had meant it as ascending from 1 to 100 but you do what you like 🙂 62 characters is extremely good, though one problem is that it prints the zero at the end (or rather, it prints ‘FizzBuzz’)

  13. unscriptable says:

    This one was fun to devise, but is way too long to be a contender:

    (function t(i){console.log(‘FizzBuzz’.slice(s=i%3&&4,i%5?4-s:8)||i);i<100&&t(i+1)})(1)


  14. kaigan says:

    @LJHarb, have you tried texting that piece of JS, it’s a pain, even on the iPhone! 😉

  15. unscriptable says:

    Another solution, but still not short enough:


  16. unscriptable says:

    Those C guys knew what they were doing when they perfected the for loop. I can’t find anything shorter, even this recursive monster (FF 3+ only):

    (function t(i)i&&(t(i-1),console.log((i%3?”:’Fizz’)+(i%5?”:’Buzz’)||i)))(100)

  17. I posted the slice one in twitter before unscriptable comment and mine was just 66:


    the most logic/short has been already posted though, so I guess game over


  18. Pingback: FizzBuzz and Ternary Explorations | Never Trust User Input

  19. Pingback: 30 Days To Dream Job- The Interview-Day 9 | Coding Over 40

Leave a Reply

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

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

Facebook photo

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

Connecting to %s

%d bloggers like this: