Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a safe loop that iterates over an array.
Swift
let numbers = [1, 2, 3, 4, 5] for number in [1] { print(number) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range instead of the array variable causes errors or unexpected behavior.
✗ Incorrect
Using the array name directly in the loop ensures safe iteration over its elements.
2fill in blank
mediumComplete the code to safely iterate over the indices of an array.
Swift
let fruits = ["Apple", "Banana", "Cherry"] for index in [1] { print(fruits[index]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a closed range causes an index out of range error.
✗ Incorrect
Using 0..
3fill in blank
hardFix the error in the loop to prevent out-of-bounds access.
Swift
let letters = ["a", "b", "c"] for i in 0...[1] { print(letters[i]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using letters.count causes a runtime error due to invalid index.
✗ Incorrect
The last valid index is letters.count - 1; using this prevents out-of-bounds errors.
4fill in blank
hardFill both blanks to create a dictionary comprehension that filters keys with values greater than 10.
Swift
let data = ["a": 5, "b": 15, "c": 20] let filtered = data.filter { $0.[1] > [2] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $0.key instead of $0.value causes wrong filtering.
✗ Incorrect
Accessing the value with $0.value and comparing it to 10 filters correctly.
5fill in blank
hardFill all three blanks to create a safe loop that prints uppercase keys with values less than 50.
Swift
let scores = ["math": 45, "science": 55, "english": 40] for ([1], [2]) in scores { if [3] < 50 { print([1].uppercased()) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing variable names or using incorrect comparisons causes errors.
✗ Incorrect
Using (subject, score) and checking score < 50 prints uppercase subject names safely.