GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Day 6: Elementary FizzBuzz

Forums > 100 Days of SwiftUI

Loving this course!

  1. Why is the most elementary solution to Fizzbuzz not preferable to the more sophisticated ones?

  2. How do you all post code that looks like it does on the XCode screen?

Thanks

for number in 1...100 { if number.isMultiple(of: 3) { print("Fizz") } else if number.isMultiple(of: 5) { print("Buzz") } else if number.isMultiple(of: 15) { print("Fizzbuzz") } else { print(number) } }

   

How do you all post code that looks like it does on the XCode screen?

Place backticks around your code. Either a single backtick for inline code or three backticks on the lines above and below for code blocks.

proper backtick usage

So:

for number in 1...100 { if number.isMultiple(of: 3) { print("Fizz") } else if number.isMultiple(of: 5) { print("Buzz") } else if number.isMultiple(of: 15) { print("Fizzbuzz") } else { print(number) } }

will be:

for number in 1...100 {
  if number.isMultiple(of: 3) {
    print("Fizz")
  } else if number.isMultiple(of: 5) {
    print("Buzz")
  } else if number.isMultiple(of: 15) {
    print("Fizzbuzz")
  } else {
    print(number)
  }
}

3      

Hello @LawOfFives,

I’m glad you’re enjoying the course and finding SwiftUI interesting! Using 15 instead of checking for both 3 and 5 is a smart move for simplicity. However, there’s an issue with the current order of conditions in your code. This issue affects how many times "Fizzbuzz" is actually printed to the console.

What’s the Issue?

The problem is that the checks for 3 and 5 are evaluated before the check for 15. When a number like 15 (or 30, 45, etc.) comes up, the first if condition that checks number.isMultiple(of: 3) is true, so "Fizz" is printed, and the rest of the conditions are skipped. Because of this, "Fizzbuzz" will never be printed to the console.

How Many "Fizzbuzz" are Printed?

With the current code, no "Fizzbuzz" will be printed because numbers that are multiples of both 3 and 5 will only match the first condition (isMultiple(of: 3)) and print "Fizz".

To make sure "Fizzbuzz" is printed for numbers that are multiples of both 3 and 5 (or 15), you need to check this condition first, before checking for just 3 or 5.

2      

To add to MartinAtElitapars response. Ternary operators are a great thing too......

for x in 1...100
{print (x.isMultiple(of: 15) ? ("FizzBuzz"):
           (x.isMultiple(of: 5) ? ("Buzz"):
           (x.isMultiple(of: 3) ? ("Fizz"):"\(x)")))
}

2      

Great responses guys, thanks!

I did have the multiple of 15 check up front when I solved it, but then overwrote it with the video answer, and didnt re-do it correctly.

1      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.