0
0
Swiftprogramming~10 mins

Enum methods and computed properties in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a computed property that returns a description for the enum case.

Swift
enum Direction {
    case north, south, east, west
    var description: String {
        switch self {
        case .north: return "Going [1]"
        case .south: return "Going south"
        case .east: return "Going east"
        case .west: return "Going west"
        }
    }
}
Drag options to blanks, or click blank then click option'
Aleft
Bup
Cnorth
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using a direction word that doesn't match the case name.
Leaving the placeholder blank.
2fill in blank
medium

Complete the method to return true if the direction is vertical (north or south).

Swift
enum Direction {
    case north, south, east, west
    func isVertical() -> Bool {
        return self == .[1] || self == .south
    }
}
Drag options to blanks, or click blank then click option'
Aup
Beast
Cwest
Dnorth
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'east' or 'west' which are horizontal directions.
Using a non-existent case like 'up'.
3fill in blank
hard

Fix the error in the method that returns the opposite direction.

Swift
enum Direction {
    case north, south, east, west
    func opposite() -> Direction {
        switch self {
        case .north: return .[1]
        case .south: return .north
        case .east: return .west
        case .west: return .east
        }
    }
}
Drag options to blanks, or click blank then click option'
Asouth
Bnorth
Ceast
Dwest
Attempts:
3 left
💡 Hint
Common Mistakes
Returning .north for the .north case causing no change.
Returning east or west which are not opposites of north.
4fill in blank
hard

Fill both blanks to create a computed property that returns true if the direction is horizontal.

Swift
enum Direction {
    case north, south, east, west
    var isHorizontal: Bool {
        return self == .[1] || self == .[2]
    }
}
Drag options to blanks, or click blank then click option'
Aeast
Bnorth
Cwest
Dsouth
Attempts:
3 left
💡 Hint
Common Mistakes
Using north or south which are vertical directions.
Swapping the order of east and west is acceptable but must match correct_answer.
5fill in blank
hard

Fill all three blanks to create a method that returns a string with the direction and its opposite.

Swift
enum Direction {
    case north, south, east, west
    func descriptionWithOpposite() -> String {
        let opposite: Direction
        switch self {
        case .north: opposite = .[1]
        case .south: opposite = .north
        case .east: opposite = .west
        case .west: opposite = .east
        }
        return "\([2]\) is opposite to \([3]\)"
    }
}
Drag options to blanks, or click blank then click option'
Asouth
Bself
Copposite
Dnorth
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong opposite direction for .north.
Using literal strings instead of variables in the return statement.