Complete the code to print each element in the array.
let fruits = ["Apple", "Banana", "Cherry"] for fruit in [1] { print(fruit) }
The loop iterates over the array named fruits. So, fruits is the correct variable to use.
Complete the code to print the index and value of each element using enumerated().
let colors = ["Red", "Green", "Blue"] for (index, color) in colors.[1]() { print("\(index): \(color)") }
The enumerated() method returns a sequence of pairs (index, element) for arrays.
Fix the error in the loop to correctly access index and value.
let numbers = [10, 20, 30] for ([1], value) in numbers.enumerated() { print("Index: \([1]), Value: \(value)") }
The variable index is used to hold the index from enumerated(). It must be consistent in the loop and print statement.
Fill both blanks to create a dictionary with words as keys and their lengths as values, filtering words longer than 4 letters.
let words = ["apple", "dog", "banana", "cat"] let wordLengths = [[1]: [2] for word in words where word.count > 4]
word.length which is not valid in Swift.The dictionary comprehension uses word as the key and word.count as the value. The filter keeps words longer than 4 letters.
Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, filtering words with length greater than 3.
let words = ["sun", "moon", "star", "sky"] let result = [[1]: [2] for word in words where [3]]
word.lowercased() instead of uppercase.The dictionary keys are uppercase words using word.uppercased(). Values are word lengths with word.count. The filter keeps words longer than 3 letters.