Fizz Buzz

@danielstucke asked for little programming tasks around conditionals and loops.

I suggested the Fizz Buzz challenge.

is a group word game frequently encountered as a drinking game. Players take turns to count incrementally, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz”.

Before reading on, try it. Go on, write some code that plays Fizz Buzz.

Here’s an example in Javascript:

for ( i = 1; i <101; i++) {
    if (i % 15 == 0) {
    } else
    if (i % 3 == 0) {
    } else
    if (i % 5 == 0) {
    } else {
        document.write(i + "<br>");

You can even play the game with Scratch.

FizzBuzz in Scratch from

Jeff (of CodingHorror and StackOverflow fame) has a great post on the challenges of recruiting programmers that can program where he talks about FizzBuzz as a screening tool. The comments get diverted from the topic of the post to implementations of FizzBuzz (resulting in this follow-up post) but what’s illuminating is the number of FizzBuzz implementations in the comments that don’t meet the specification for the game.

Lest we smile too much at that observation, Alberto Savoia writing in Beautiful Code observed:

…Donald Knuth points out that even though the first binary search [algorithm] was published in 1946, it took 12 more years for the first binary search without bugs to be published.

Writing correct code is hard.

Bonus Questions:

  1. What does a binary search algorithm do?
  2. Donald Knuth is famous for many things. My favourite is the version numbering for TeX, the typesetting system Knuth developed when unsatisfied with the typography in his series The Art of Computer Programming. What’s so interesting about the version number for TeX?
  3. In 2006 a bug in a commonly implemented version of the binary search algorithm was found. In which programming language was the bug first reported for?

One thought on “Fizz Buzz

  1. Great lesson doing this in Scratch today. Great to see students learn to nest their If statements to stop the program saying FizzBuzz and Fizz and Buzz instead of just FizzBuzz. Have used Scratch for years making little games but increasingly impressed with more traditional coding tasks that can be completed with it. 🙂

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 )

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