Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to match the number 10 in the switch statement.
Swift
let number = 10 switch number { case [1]: print("Number is ten") default: print("Number is something else") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a number different from 10 will not match the case.
✗ Incorrect
The case must match the value 10 to print "Number is ten".
2fill in blank
mediumComplete the where clause to check if the number is even.
Swift
let number = 8 switch number { case let x where x [1] 2 == 0: print("Even number") default: print("Odd number") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of % will not check evenness.
✗ Incorrect
The modulo operator (%) checks the remainder. If remainder is 0 when divided by 2, the number is even.
3fill in blank
hardFix the error in the where clause to check if the number is positive.
Swift
let number = -3 switch number { case let x where x [1] 0: print("Positive number") default: print("Non-positive number") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will check for negative numbers instead.
✗ Incorrect
To check if a number is positive, it must be greater than 0.
4fill in blank
hardFill both blanks to check if the number is between 1 and 10 inclusive.
Swift
let number = 7 switch number { case let x where x [1] 1 && x [2] 10: print("Number is between 1 and 10") default: print("Number is out of range") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < only excludes the boundary numbers.
✗ Incorrect
The number should be greater than or equal to 1 and less than or equal to 10.
5fill in blank
hardFill both blanks to check if the number is odd and greater than 5.
Swift
let number = 7 switch number { case let x where x [1] 2 != 0 && x [2] 5: print("Odd number greater than 5") default: print("Other number") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == 0 checks even numbers, not odd.
✗ Incorrect
The modulo operator % checks oddness (not equal to 0), and > 5 checks if number is greater than 5.