Challenge - 5 Problems
Swift For-in Where Clause Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a Swift for-in loop with where clause filtering even numbers
What will be printed by this Swift code?
let numbers = [1, 2, 3, 4, 5, 6]
for num in numbers where num % 2 == 0 {
print(num)
}Swift
let numbers = [1, 2, 3, 4, 5, 6] for num in numbers where num % 2 == 0 { print(num) }
Attempts:
2 left
💡 Hint
The where clause filters elements before the loop body runs.
✗ Incorrect
The where clause filters the numbers array to only even numbers (num % 2 == 0). So only 2, 4, and 6 are printed.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Swift for-in loop with where clause
Which option correctly fixes the syntax error in this code?
let fruits = ["apple", "banana", "cherry"]
for fruit in fruits where fruit.hasPrefix("a")
print(fruit)
Swift
let fruits = ["apple", "banana", "cherry"] for fruit in fruits where fruit.hasPrefix("a") print(fruit)
Attempts:
2 left
💡 Hint
Swift requires braces for loop bodies with multiple lines or where clauses.
✗ Incorrect
The for-in loop with a where clause must have its body enclosed in curly braces. Option D adds the braces correctly.
❓ optimization
advanced2:00remaining
Optimize filtering in a Swift for-in loop with where clause
Given this code:
Which option is the most efficient way to calculate the sum of multiples of 5?
let numbers = Array(1...1000)
var sum = 0
for num in numbers where num % 5 == 0 {
sum += num
}Which option is the most efficient way to calculate the sum of multiples of 5?
Swift
let numbers = Array(1...1000) var sum = 0 for num in numbers where num % 5 == 0 { sum += num }
Attempts:
2 left
💡 Hint
Avoid checking every number when you can jump directly to multiples.
✗ Incorrect
Using stride lets you loop only over multiples of 5, avoiding unnecessary checks and improving performance.
🔧 Debug
advanced2:00remaining
Debug why this Swift for-in loop with where clause prints no output
Consider this code:
Why does it print nothing?
let words = ["Swift", "is", "fun"]
for word in words where word.count > 10 {
print(word)
}Why does it print nothing?
Swift
let words = ["Swift", "is", "fun"] for word in words where word.count > 10 { print(word) }
Attempts:
2 left
💡 Hint
Check the condition in the where clause against the array elements.
✗ Incorrect
All words have fewer than or equal to 10 characters, so none satisfy the condition word.count > 10, resulting in no output.
🧠 Conceptual
expert3:00remaining
Understanding the behavior of multiple where clauses in nested for-in loops
What will be the output of this Swift code?
let numbers = [1, 2, 3]
let letters = ["a", "b", "c"]
for num in numbers where num % 2 != 0 {
for letter in letters where letter != "b" {
print("\(num)\(letter)")
}
}Swift
let numbers = [1, 2, 3] let letters = ["a", "b", "c"] for num in numbers where num % 2 != 0 { for letter in letters where letter != "b" { print("\(num)\(letter)") } }
Attempts:
2 left
💡 Hint
Both loops filter elements with their own where clauses.
✗ Incorrect
The outer loop runs only for odd numbers (1 and 3). The inner loop skips letter "b". So combinations printed are 1a, 1c, 3a, 3c.