Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a stride from 0 to 10 with a step of 2.
Swift
for number in stride(from: 0, to: 10, by: [1]) { print(number) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a step larger than the range, which results in fewer numbers.
Using 'to' instead of 'through' when you want to include the end number.
✗ Incorrect
The stride(from:to:by:) function creates a sequence starting at 0, up to but not including 10, stepping by 2.
2fill in blank
mediumComplete the code to create a stride from 1 through 9 with a step of 2.
Swift
for number in stride(from: 1, through: 9, by: [1]) { print(number) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to' instead of 'through' which excludes the last number.
Using a step that skips the last number.
✗ Incorrect
Using stride(from:through:by:) with step 2 includes the last number 9 if it fits the sequence.
3fill in blank
hardFix the error in the stride to count down from 10 to 0 by 2.
Swift
for number in stride(from: 10, through: 0, by: [1]) { print(number) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a positive step when counting down causes an infinite loop or no output.
Using -1 instead of -2 changes the step size.
✗ Incorrect
When counting down, the step must be negative. Here, -2 counts down by 2.
4fill in blank
hardFill both blanks to create a stride from 5 to 15 with a step of 3.
Swift
for number in stride(from: [1], to: [2], by: 3) { print(number) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 15 as the end excludes numbers up to 15.
Using 'through' instead of 'to' changes the inclusion of the end number.
✗ Incorrect
The stride starts at 5 and goes up to but not including 10, stepping by 3.
5fill in blank
hardFill all three blanks to create a stride from 20 down to 10 with a step of 2.
Swift
for number in stride(from: [1], through: [2], by: [3]) { print(number) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a positive step when counting down.
Using 'to' instead of 'through' to include the end number.
✗ Incorrect
The stride counts down from 20 through 10 by steps of -2.