fizzbuzz šŸ’»

I’m trying to learn Apple’s Swift programming language the only way I know how to learn a programming language–by smacking a keyboard with my fingers until something works.


I completed A-level Computing back in college which taught me a couple of valuable lessons:

  • VB.NET is a cruel mistress, but Stockholm Syndrome has me only remembering the good times

  • It’s not about being able to program; it’s about being able to problem solve

I’ve seen quite a few people online raving about Swift (particularly the videos Apple uploaded showing everyone from babies to grandparents writing apps in Swift) and had some spare hours between work, sleep, and Crash Bandicoot; so I’ve started to try pick up Swift to see what I can accomplish.

Apple has a fantastic free eBook they keep updated for learning the basics of Swift as well as a website they maintain with a primer as well as a PHP.net style function breakdown. It goes without saying that documentation is king.

so wtf is fizzbuzz

FizzBuzz is a children’s game which some companies use in interviews when hiring programmers.

FizzBuzz is a game where two people take turns reciting numbers from 1-100 (or higher). If a number is a multiple of 3 the person must say ā€œFizzā€ instead, if a number is a multiple of 5: ā€œBuzzā€, and for both ā€œFizzBuzzā€. For example:

  • 1
  • 2
  • fizz
  • 3
  • buzz
  • fizz
  • 7
  • 8
  • fizz
  • buzz
  • 11
  • fizz
  • 13
  • 14
  • fizzbuzz

Asking a programmer to reproduce this should be a very easy task, but it does also provide a great insight into what style of programmer they are. Do they slap shit together and hope for the best? Or do they actually think of edge cases and future-proofing?

There’s tonnes of great resources online on FizzBuzz, including this video by Tom Scott on the various methods people use to solve FizzBuzz. My method is below, in Swift.

for i in 1...100 {
    var output = ""
    if i % 3 == 0 { output += "Fizz" }
    if i % 5 == 0 { output += "Buzz" }
    if output == "" { output = String(i) }
    print(output)
}

And here’s the output:

fizzbuzz

My blog does not host comments. If you want to discuss a post, or say hello, you can hit me up on BlueSky where I'm @ThomasChatting.dev or you can email me at hello (at) thomaschatting (dot) dev.