Complete the code to print only even numbers from the array.
let numbers = [1, 2, 3, 4, 5, 6] for number in numbers where number [1] 2 == 0 { print(number) }
The where clause filters numbers where the remainder when divided by 2 is zero, meaning even numbers.
Complete the code to print names that start with the letter 'A'.
let names = ["Alice", "Bob", "Anna", "Mark"] for name in names where name.hasPrefix([1]) { print(name) }
The hasPrefix method checks if the string starts with the given substring. Here, we want names starting with 'A'.
Fix the error in the code to print numbers greater than 10.
let values = [5, 12, 18, 7, 3] for val in values where val [1] 10 { print(val) }
The where clause should filter values greater than 10, so the operator must be '>'.
Fill both blanks to print words longer than 4 characters.
let words = ["apple", "dog", "banana", "cat"] for word in words where word.[1] > [2] { print(word) }
The count property gives the length of the string. We compare it to 4 to find words longer than 4 characters.
Fill all three blanks to print numbers divisible by 3 and greater than 10.
let nums = [3, 9, 12, 15, 7, 20] for num in nums where num [1] 3 == 0 && num [2] [3] { print(num) }
The modulo operator '%' checks divisibility by 3. The 'where' clause also checks if the number is greater than 10.