Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to match multiple cases in one line.
Swift
let fruit = "apple" switch fruit { case [1]: print("It's a citrus fruit.") default: print("It's another fruit.") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to separate values with commas.
Using parentheses instead of commas.
Not enclosing strings in quotes.
✗ Incorrect
In Swift, you can match multiple values in a single case by separating them with commas inside the case statement.
2fill in blank
mediumComplete the switch statement to print "Weekend" for Saturday and Sunday.
Swift
let day = "Sunday" switch day { case [1]: print("Weekend") default: print("Weekday") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including weekdays in the weekend case.
Forgetting to use commas between days.
Not enclosing day names in quotes.
✗ Incorrect
Saturday and Sunday are weekend days, so they should be grouped in the same case separated by commas.
3fill in blank
hardFix the error in the switch case to correctly match multiple values.
Swift
let number = 3 switch number { case [1]: print("Number is 1, 2, or 3") default: print("Other number") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using logical OR (||) instead of commas.
Using array brackets [] in case.
Using logical AND (&&) instead of commas.
✗ Incorrect
In Swift switch cases, multiple values are separated by commas, not logical operators or array brackets.
4fill in blank
hardFill both blanks to create a switch that prints "Vowel" for 'a', 'e', 'i', 'o', 'u' and "Consonant" for 'b', 'c', 'd'.
Swift
let letter: Character = "a" switch letter { case [1]: print("Vowel") case [2]: print("Consonant") default: print("Other letter") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing vowels and consonants in the same case.
Using parentheses instead of commas.
Not enclosing characters in single quotes.
✗ Incorrect
The first case matches vowels separated by commas; the second case matches consonants similarly.
5fill in blank
hardFill all three blanks to create a switch that prints "Small", "Medium", or "Large" for sizes 1-2, 3-4, and 5-6 respectively.
Swift
let size = 4 switch size { case [1]: print("Small") case [2]: print("Medium") case [3]: print("Large") default: print("Unknown size") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ranges like 1...2 instead of listing values separated by commas.
Mixing sizes between cases.
Not separating values with commas.
✗ Incorrect
Each case matches a range of sizes separated by commas to group them correctly.