Complete the code to use a guard statement that exits early if the number is not positive.
func checkNumber(_ number: Int) {
guard [1] else {
print("Number is not positive")
return
}
print("Number is positive")
}The guard statement checks if the number is greater than zero. If not, it exits early.
Complete the guard statement to unwrap the optional string safely.
func greet(_ name: String?) {
guard let unwrappedName = [1] else {
print("No name provided")
return
}
print("Hello, \(unwrappedName)!")
}The guard statement unwraps the optional 'name' safely using 'let'.
Fix the error in the guard statement to check if the array is not empty.
func process(items: [Int]) {
guard [1] else {
print("No items to process")
return
}
print("Processing \(items.count) items")
}The guard statement should check that the array is NOT empty using '!items.isEmpty'.
Fill both blanks to unwrap an optional integer and check it is positive.
func validate(_ value: Int?) {
guard let number = [1], number [2] 0 else {
print("Invalid number")
return
}
print("Valid number: \(number)")
}First unwrap the optional 'value' with 'let'. Then check if 'number' is greater than zero.
Fill all three blanks to unwrap two optionals and check the second is not empty.
func process(user: String?, email: String?) {
guard let username = [1], let userEmail = [2], !userEmail.[3] else {
print("Missing or empty email")
return
}
print("User: \(username), Email: \(userEmail)")
}Unwrap 'user' and 'email' optionals, then check that 'userEmail' is not empty using '!userEmail.isEmpty'.