0
0
Swiftprogramming~10 mins

For-in loop with ranges 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 numbers from 1 to 5.

Swift
for number in [1] {
    print(number)
}
Drag options to blanks, or click blank then click option'
A1 to 5
B1..5
C1...5
Drange(1,5)
Attempts:
3 left
💡 Hint
Common Mistakes
Using two dots instead of three dots for range.
Using Python-style range syntax.
2fill in blank
medium

Complete the code to print numbers from 1 up to but not including 5.

Swift
for number in [1] {
    print(number)
}
Drag options to blanks, or click blank then click option'
A1..<5
Brange(1,5)
C1..5
D1...5
Attempts:
3 left
💡 Hint
Common Mistakes
Using closed range ... which includes the end value.
Using invalid range syntax.
3fill in blank
hard

Fix the error in the code to print numbers from 1 to 3.

Swift
for i in [1] {
    print(i)
}
Drag options to blanks, or click blank then click option'
A1<3
B1...3
C1..3
Drange(1,3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using two dots instead of three dots.
Using Python range syntax.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

Swift
for number in [1] {
    if number [2] 2 == 0 {
        print(number)
    }
}
Drag options to blanks, or click blank then click option'
A2...10
B%
C==
D2..<10
Attempts:
3 left
💡 Hint
Common Mistakes
Using half-open range which excludes 10.
Using equality operator instead of modulo.
5fill in blank
hard

Fill all three blanks to create a dictionary of numbers and their squares for numbers 1 to 5, but only include squares greater than 10.

Swift
var squares = [Int: Int]()
for number in [3] {
    if number * number > 10 {
        squares[[1]] = [2]
    }
}
Drag options to blanks, or click blank then click option'
Anumber
Bnumber * number
C1...5
Dnumber * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable for keys or values.
Using half-open range excluding 5.
Using multiplication by 2 instead of square.