Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a direction word that doesn't match the case name.
Leaving the placeholder blank.
✗ Incorrect
The computed property returns a string describing the enum case. For the .north case, it should return "Going north".
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'east' or 'west' which are horizontal directions.
Using a non-existent case like 'up'.
✗ Incorrect
The method checks if the direction is vertical by comparing to .north or .south. The blank must be 'north'.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The opposite of north is south, so the method should return .south for the .north case.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Horizontal directions are east and west, so the computed property returns true if self is .east or .west.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong opposite direction for .north.
Using literal strings instead of variables in the return statement.
✗ Incorrect
The method assigns opposite for .north as .south, then returns a string using 'self' and 'opposite' to describe the relation.