0
0
Swiftprogramming~10 mins

Adding 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 add a computed property that returns the full name.

Swift
struct Person {
    var firstName: String
    var lastName: String
    var fullName: String {
        return firstName [1] lastName
    }
}
Drag options to blanks, or click blank then click option'
A+ " " +
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using arithmetic operators like '-' or '*' instead of '+' for strings.
Forgetting to add a space between first and last names.
2fill in blank
medium

Complete the code to add a computed property that calculates 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 addition instead of multiplication.
Using division or subtraction which are incorrect for area calculation.
3fill in blank
hard

Fix the error in the computed property that returns the age in months.

Swift
struct Person {
    var ageInYears: Int
    var ageInMonths: Int {
        return ageInYears [1] 12
    }
}
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using division which would give incorrect results.
4fill in blank
hard

Fill both blanks to create a computed property that returns the perimeter of a rectangle.

Swift
struct Rectangle {
    var width: Double
    var height: Double
    var perimeter: Double {
        return (width [1] height) [2] 2
    }
}
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying width and height instead of adding.
Forgetting to multiply the sum by 2.
5fill in blank
hard

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

Swift
struct Person {
    var firstName: String
    var lastName: String
    var initials: String {
        return firstName[1].uppercased() [2] lastName[3].uppercased()
    }
}
Drag options to blanks, or click blank then click option'
A[first]
B+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition to join strings.
Not using subscript to get the first character.
Not converting characters to uppercase.