0
0
Swiftprogramming~10 mins

Array iteration and enumerated in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print each element in the array.

Swift
let fruits = ["Apple", "Banana", "Cherry"]
for fruit in [1] {
    print(fruit)
}
Drag options to blanks, or click blank then click option'
Afruits
Bfruit
Carray
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop variable name instead of the array name.
Using a variable name that is not declared.
2fill in blank
medium

Complete the code to print the index and value of each element using enumerated().

Swift
let colors = ["Red", "Green", "Blue"]
for (index, color) in colors.[1]() {
    print("\(index): \(color)")
}
Drag options to blanks, or click blank then click option'
Aenumerated
Benumerate
Cindexed
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist like 'enumerate' or 'indexed'.
Forgetting the parentheses after the method name.
3fill in blank
hard

Fix the error in the loop to correctly access index and value.

Swift
let numbers = [10, 20, 30]
for ([1], value) in numbers.enumerated() {
    print("Index: \([1]), Value: \(value)")
}
Drag options to blanks, or click blank then click option'
Ai
Bvalue
Cnum
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the index in the loop and print statement.
Using the value variable name instead of index.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, filtering words longer than 4 letters.

Swift
let words = ["apple", "dog", "banana", "cat"]
let wordLengths = [[1]: [2] for word in words where word.count > 4]
Drag options to blanks, or click blank then click option'
Aword
Bword.count
Cword.length
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.length which is not valid in Swift.
Using a variable name not declared.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, filtering words with length greater than 3.

Swift
let words = ["sun", "moon", "star", "sky"]
let result = [[1]: [2] for word in words where [3]]
Drag options to blanks, or click blank then click option'
Aword.uppercased()
Bword.count
Cword.count > 3
Dword.lowercased()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lowercased() instead of uppercase.
Using incorrect filter condition.