UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Checkpoint 3

Paul Hudson    @twostraws   

Updated for Xcode 15

Now that you’re able to use conditions and loops, I’d like you to try a classic computer science problem. It’s not hard to understand, but it might take you a little time to solve depending on your prior experience!

The problem is called fizz buzz, and has been used in job interviews, university entrance tests, and more for as long as I can remember. Your goal is to loop from 1 through 100, and for each number:

  1. If it’s a multiple of 3, print “Fizz”
  2. If it’s a multiple of 5, print “Buzz”
  3. If it’s a multiple of 3 and 5, print “FizzBuzz”
  4. Otherwise, just print the number.

So, here are some example values you should have when your code runs:

  • 1 should print “1”
  • 2 should print “2”
  • 3 should print “Fizz”
  • 4 should print “4”
  • 5 should print “Buzz”
  • 6 should print “Fizz”
  • 7 should print “7”
  • 15 should print “FizzBuzz”
  • 100 should print “Buzz”

Before you start: This problem seems extremely simple, but many, many developers struggle to solve it. I’ve seen it happen personally, so don’t stress about it – just trying to solve the problem already teaches you about it.

You already know everything you need to solve that problem, but if you’d like some hints then I’ll add some below.

Please go ahead and try building the playground now.

Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Checkpoint 3. If you don’t already subscribe, you can start a free trial today.

Still here? Okay, here are some hints:

  1. You can check whether one number is a multiple of another by using .isMultiple(of:). For example, i.isMultiple(of: 3).
  2. In this instance you need to check for 3 and 5 first because it’s the most specific, then 3, then 5, and finally have an else block to handle all other numbers.
  3. You can either use && to check for numbers that are multiples of 3 and 5, or have a nested if statement.
  4. You need to count from 1 through 100, so use ... rather than ..<.
Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.8/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.