Let's use an if/else control flow statement to write a function to return the right pool ball type:
- Create an enum to describe the possible ball types:
enum PoolBallType {
case solid
case stripe
case black
}
- Create the method that will take an Int and return PoolBallType:
func poolBallType(forNumber number: Int) -> PoolBallType {
if number < 8 {
return .solid
} else if number > 8 {
return .stripe
} else {
return .black
}
}
- Use this function and test that we get the expected results:
let two = poolBallType(forNumber: 2) // .solid
let eight = poolBallType(forNumber: 8) // .black
let twelve = poolBallType(forNumber: 12) // .stripe