0
0
Swiftprogramming~10 mins

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 declare a computed property that returns the double of the stored property.

Swift
struct Number {
    var value: Int
    var doubleValue: Int {
        return value [1] 2
    }
}
Drag options to blanks, or click blank then click option'
A/
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * will add 2 instead of doubling.
Using / will divide instead of multiply.
2fill in blank
medium

Complete the code to create a computed property that returns the full name by combining first and last names.

Swift
struct Person {
    var firstName: String
    var lastName: String
    var fullName: String {
        return firstName [1] " " [1] lastName
    }
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using * or - operators with strings causes errors.
Forgetting to add a space between names.
3fill in blank
hard

Fix the error in the computed property to correctly calculate the area of a rectangle.

Swift
struct Rectangle {
    var width: Double
    var height: Double
    var area: Double {
        return width [1] height
    }
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + adds dimensions instead of calculating area.
Using / or - gives wrong results.
4fill in blank
hard

Fill both blanks to create a computed property that returns true if the number is even.

Swift
struct NumberChecker {
    var number: Int
    var isEven: Bool {
        return number [1] 2 [2] 0
    }
}
Drag options to blanks, or click blank then click option'
A%
B==
C!=
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using / instead of % to get remainder.
Using != instead of == causes wrong logic.
5fill in blank
hard

Fill all three blanks to create a computed property that returns the initials of a person in uppercase.

Swift
struct Person {
    var firstName: String
    var lastName: String
    var initials: String {
        return firstName[1].uppercased() + lastName[2].uppercased()
    }
}
Drag options to blanks, or click blank then click option'
Aprefix(1)
Bsuffix(1)
Cprefix(0)
DdropFirst()
Attempts:
3 left
💡 Hint
Common Mistakes
Using suffix(1) gets last letter, not initial.
Using prefix(0) returns empty string.
Using dropFirst() removes first letter instead of selecting it.