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

Checkpoint 3 - solution question

Forums > 100 Days of SwiftUI

Hi,

i wrote some code for the solution but i im not sure it's quite ok because it's printing number (i) AND strings (Buzz, Fizz, FizzBuzz) - shouldn't be printed number OR strings?

for i in 1...100 {
    if i.isMultiple(of: 3) {
        print("Fizz")
    }
    if i.isMultiple(of: 5) {
        print("Buzz")
    }
    if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
        print("FizzBuzz")
    }
    else {
        print(i)
    }

}

Printing: 1 2 Fizz 3 4 Buzz 5 Fizz 6 7 8 Fizz 9 Buzz 10

2      

Filip has a brain teaser:

im not sure it's quite ok because it's printing number (i) AND strings

Well, one lesson to remember: SwiftUI is doing exactly what you tell it to do.

If you reformat your code, you'll see why:

for i in 1...100 {
    if i.isMultiple(of: 3) {
        print("Fizz")      // Fizz: ONLY if multiple of 3.
    }
    if i.isMultiple(of: 5) {
        print("Buzz")      // Buzz: ONLY if multiple of 5
    }
    // ------------- The if and else belong together ------
    if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
        print("FizzBuzz") // FizzBuzz: ONLY if multiple of 3 and multiple of 5
    } else {
        print(i)          // Print(i) if NOT a multiple of 3 and a multiple of 5
    }
}

Keep Coding!

2      

Ok sooo :)

I think I have use else if:

for i in 1...100 {
    if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
        print("FizzBuzz")
    } else if i.isMultiple(of: 3) {
        print("Fizz")
    } else if i.isMultiple(of: 5) {
        print("Buzz")
    } else {
        print(i) 
    }
}

2      

Filip wins! Well done.

To help you debug, or as a confirmation, you might change your output to show the integer you're evaluating. Something like this:

print("------ Fizz Buzz ---------")
for i in 1...100 {
    if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
        print("\(i) FizzBuzz")    // <-- Show the integer
    } else if i.isMultiple(of: 3) {
        print("\(i) Fizz")        // <-- Show the integer
    } else if i.isMultiple(of: 5) {
        print("\(i) Buzz")        // <-- Show the integer
    } else {
        print("\(i) ---")         // <-- Show the integer
    }
}

Then your output will help confirm your design:

------ Fizz Buzz ---------
1 ---
2 ---
3 Fizz
4 ---
5 Buzz
6 Fizz
7 ---
8 ---
9 Fizz
10 Buzz
11 ---
12 Fizz
13 ---
14 ---
15 FizzBuzz
16 ---
17 ---
18 Fizz
..... snip ......

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.