Challenge - 5 Problems
Range Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inclusive range loop
What is the output of this Swift code using the inclusive range operator
...?Swift
for i in 1...3 { print(i, terminator: ",") }
Attempts:
2 left
💡 Hint
Inclusive range includes both start and end values.
✗ Incorrect
The
... operator creates a range that includes both the start and the end values. So the loop runs for 1, 2, and 3.❓ Predict Output
intermediate2:00remaining
Output of half-open range loop
What is the output of this Swift code using the half-open range operator
..<?Swift
for i in 1..<4 { print(i, terminator: "-") }
Attempts:
2 left
💡 Hint
Half-open range excludes the end value.
✗ Incorrect
The
..< operator creates a range that includes the start but excludes the end. So the loop runs for 1, 2, and 3.🔧 Debug
advanced2:00remaining
Why does this range cause a runtime error?
Consider this Swift code snippet:
let numbers = [10, 20, 30]
for i in 0...numbers.count {
print(numbers[i])
}
What error will this code cause when run?Attempts:
2 left
💡 Hint
Check the range bounds against the array indices.
✗ Incorrect
The
0...numbers.count range includes the value 3, but the array indices go from 0 to 2. Accessing numbers[3] causes an index out of range error.🧠 Conceptual
advanced2:00remaining
Count of elements in a half-open range
How many elements are in the range
5..<10 in Swift?Attempts:
2 left
💡 Hint
Count is end minus start for half-open ranges.
✗ Incorrect
The half-open range
5..<10 includes 5,6,7,8,9 which is 5 elements.🚀 Application
expert3:00remaining
Using range operators to slice arrays
Given
let arr = ["a", "b", "c", "d", "e"], which option correctly extracts the subarray ["b", "c", "d"] using range operators?Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and half-open ranges exclude the upper bound.
✗ Incorrect
The subarray ["b", "c", "d"] corresponds to indices 1, 2, and 3. The half-open range 1..<4 includes 1, 2, and 3.