Complete the code to declare a computed property that returns the double of the stored property.
struct Number {
var value: Int
var doubleValue: Int {
return value [1] 2
}
}The computed property doubleValue returns value multiplied by 2 using the * operator.
Complete the code to create a computed property that returns the full name by combining first and last names.
struct Person {
var firstName: String
var lastName: String
var fullName: String {
return firstName [1] " " [1] lastName
}
}The computed property fullName concatenates firstName, a space, and lastName using the + operator.
Fix the error in the computed property to correctly calculate the area of a rectangle.
struct Rectangle {
var width: Double
var height: Double
var area: Double {
return width [1] height
}
}The area of a rectangle is width multiplied by height, so the * operator is correct.
Fill both blanks to create a computed property that returns true if the number is even.
struct NumberChecker {
var number: Int
var isEven: Bool {
return number [1] 2 [2] 0
}
}The computed property uses the modulus operator % to get the remainder when dividing by 2, then checks if it is equal to 0 with ==.
Fill all three blanks to create a computed property that returns the initials of a person in uppercase.
struct Person {
var firstName: String
var lastName: String
var initials: String {
return firstName[1].uppercased() + lastName[2].uppercased()
}
}The computed property uses prefix(1) to get the first letter of each name, then converts it to uppercase.