0
0
Swiftprogramming~10 mins

For-in with where clause 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 only even numbers from the array.

Swift
let numbers = [1, 2, 3, 4, 5, 6]
for number in numbers where number [1] 2 == 0 {
    print(number)
}
Drag options to blanks, or click blank then click option'
A+
B%
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '%' causes a syntax error.
Using '-' or '*' does not correctly check for even numbers.
2fill in blank
medium

Complete the code to print names that start with the letter 'A'.

Swift
let names = ["Alice", "Bob", "Anna", "Mark"]
for name in names where name.hasPrefix([1]) {
    print(name)
}
Drag options to blanks, or click blank then click option'
A"B"
B"M"
C"Z"
D"A"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a letter other than 'A' will filter wrong names.
Forgetting quotes causes a syntax error.
3fill in blank
hard

Fix the error in the code to print numbers greater than 10.

Swift
let values = [5, 12, 18, 7, 3]
for val in values where val [1] 10 {
    print(val)
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' filters numbers less than or equal to 10.
Using '==' filters only numbers equal to 10.
4fill in blank
hard

Fill both blanks to print words longer than 4 characters.

Swift
let words = ["apple", "dog", "banana", "cat"]
for word in words where word.[1] > [2] {
    print(word)
}
Drag options to blanks, or click blank then click option'
Acount
Blength
C4
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' is incorrect in Swift strings.
Comparing to 5 filters words longer than 5 characters, not 4.
5fill in blank
hard

Fill all three blanks to print numbers divisible by 3 and greater than 10.

Swift
let nums = [3, 9, 12, 15, 7, 20]
for num in nums where num [1] 3 == 0 && num [2] [3] {
    print(num)
}
Drag options to blanks, or click blank then click option'
A%
B>
C10
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' changes the filter condition.
Using incorrect operators causes syntax errors.