BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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)
    }
}

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.