0
0
Swiftprogramming~10 mins

Stride for custom step 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 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'
A2
B5
C1
D3
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.
2fill in blank
medium

Complete 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'
A2
B3
C4
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to' instead of 'through' which excludes the last number.
Using a step that skips the last number.
3fill in blank
hard

Fix 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'
A-1
B2
C-2
D1
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.
4fill in blank
hard

Fill 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'
A5
B10
C15
D20
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.
5fill in blank
hard

Fill 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'
A20
B10
C-2
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a positive step when counting down.
Using 'to' instead of 'through' to include the end number.