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

SOLVED: Checkpoint 3, how to solve with switch case?

Forums > 100 Days of SwiftUI

Finish until checkpoint 3.

I just use if, else if, else and all work fine.

Now I want to try to use switch case solution... anyone have idea how to do this?

Thank you

2      

This code below did not work.

for j in 1...100 {
    switch j {
    case j.isMultiple(of: 3) && j.isMultiple(of: 5):
        print ("FizzBuzz")

    case j.isMultiple(of: 3):
        print ("Fizz")

    case j.isMultiple(of: 5):
        print ("Buzz")

    default:
        print (j)
    }
}

error: expression pattern of type "BOOL" cannot match values of type "INT"

2      

The error is clear. But you'll need a few more sessions to get used to trouble shooting. Let's help!

If you hold your option key down, and click on j, XCode will tell you that j is of type Int. Yeah, it's an integer.

for j in 1...100 {
// If you hold your option key down and click on j, 
// XCode will tell you that j is of type Int
switch j {
    case j.isMultiple(of: 3) && j.isMultiple(of: 5):
        print ("FizzBuzz")
        // more code
  } // end switch
} // end for

Now add this line just before the switch j statement: let whatsThis = j.isMultiple(of: 3)

Now hold the option key down and click on the variable whatsThis.

What does XCode reveal? What type is whatsThis?

Now you see why the compiler is not happy. You are providing an integer to the switch statement. But within the switch statement you are trying to compare j to a boolean value. You cannot compare integers to booleans!

Case solved.

PS: Whilst learning Swift use what ever variable names you wish. However, and hear me out on this, I think you lose the big picture when you use single letter names for variables.

For example, had you named the variable targetInteger your code would then read:

for targetInteger in 1...100 {
switch targetInteger {
    case targetInteger.isMultiple(of: 3) && targetInteger.isMultiple(of: 5):
        print ("FizzBuzz")
        // more code
  } // end switch
} // end for

One might argue that this is more readable. Plus it might have clued you in that you are comparing an Integer (targetInteger) to a boolean (targetInteger.isMultiple(of:) returns either true or false. Hey! that's not an integer! Just food for thought.

2      

I did checkpoint 3 two ways:

for number in 1...100 {
    switch 0 {
    case number % 3 + number % 5:
        print("FizzBuzz")
    case number % 3:
        print("Fizz")
    case number % 5:
        print("Buzz")
    default:
        print(number)
    }
}
for number in 1...100 {
    if number.isMultiple(of: 3) && number.isMultiple(of: 5) {
        print("FizzBuzz")
    } else if number.isMultiple(of: 3) {
        print("Fizz")
    } else if number.isMultiple(of: 5) {
        print("Buzz")
    } else {
        print(number)
    }
}

2      

Thank you, this give me idea to do my switch case in checkpoint 3.

This is how I did it.

for number in 1...100 {
    switch true {
    case number.isMultiple(of: 3) && number.isMultiple(of: 5):
        print("FizzBuzz")
    case number.isMultiple(of: 3):
        print("Fizz")
    case number.isMultiple(of: 5):
        print("Buzz")
    default:
        print(number)
    }
}

2      

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!

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.