Complete the code to add a computed property that returns the full name.
struct Person {
var firstName: String
var lastName: String
var fullName: String {
return firstName [1] lastName
}
}The computed property fullName combines firstName and lastName with a space in between using the + operator.
Complete the code to add a computed property that calculates the area of a rectangle.
struct Rectangle {
var width: Double
var height: Double
var area: Double {
return width [1] height
}
}The computed property area multiplies width and height to calculate the rectangle's area.
Fix the error in the computed property that returns the age in months.
struct Person {
var ageInYears: Int
var ageInMonths: Int {
return ageInYears [1] 12
}
}To convert years to months, multiply the age in years by 12.
Fill both blanks to create a computed property that returns the perimeter of a rectangle.
struct Rectangle {
var width: Double
var height: Double
var perimeter: Double {
return (width [1] height) [2] 2
}
}The perimeter of a rectangle is 2 times the sum of width and height. So first add width and height, then multiply by 2.
Fill all three blanks to create a computed property that returns the initials of a person.
struct Person {
var firstName: String
var lastName: String
var initials: String {
return firstName[1].uppercased() [2] lastName[3].uppercased()
}
}The computed property initials takes the first character of firstName and lastName, converts them to uppercase, and joins them with +.