TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Challenge

At this point you should be fairly comfortable with data types, conditions, loops, functions, and more, so your first challenge is to complete the Fizz Buzz test. This is a famous test commonly used to root out bad programmers during job interviews, and it goes like this:

  • Write a function that accepts an integer as input and returns a string.
  • If the integer is evenly divisible by 3 the function should return the string “Fizz”.
  • If the integer is evenly divisible by 5 the function should return “Buzz”.
  • If the integer is evenly divisible by 3 and 5 the function should return “Fizz Buzz”
  • For all other numbers the function should just return the input number.

To solve this challenge you’ll need to use quite a few skills you learned in this tutorial:

  1. Write a function called fizzbuzz(). It should accept an Int parameter, and return a String.
  2. You’ll need to use if and else if conditions to check the input number.
  3. You need use modulus, %, to check for even division. You’ll also need to use && to check for two things at once, because “Fizz Buzz” should only be printed if the input number is evenly divisible by 3 and 5.

Here are some test cases for you to use:

fizzbuzz(number: 3)
fizzbuzz(number: 5)
fizzbuzz(number: 15)
fizzbuzz(number: 16)

When your code is complete, that code should produce “Fizz”, “Buzz”, “Fizz Buzz” and “16”.

Please try to code your answer now. I’ve written my own solution below to get you started, but it will help you internalize Swift better if you try writing your own code first. Remember: there is no learning without struggle, so you should just try – don’t worry if you have a hard time or don’t manage to finish.

If you’re finding it hard, here are some hints. I suggest you read these only if you really need them, and even then only read one at a time – you should try to solve the challenge while reading as few hints as possible!

  • Your function should start with func fizzbuzz(number: Int) -> String {. That is, it should accept an integer and return a string.
  • You return values using the return keyword, e.g. return “Fizz”.
  • Checking that a number is evenly divisible by 3 is done using if number % 3 == 0.
  • Check that it’s evenly divisible by 3 and 5 is done using if number % 3 == 0 && number % 5 == 0.

OK, that’s it for hints. Below is my solution:

func fizzbuzz(number: Int) -> String {
    if number % 3 == 0 && number % 5 == 0 {
        return "Fizz Buzz"
    } else if number % 3 == 0 {
        return "Fizz"
    } else if number % 5 == 0 {
        return "Buzz"
    } else {
        return String(number)
    }
}
Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.