Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to match the number to its English word using a switch statement.
Swift
let number = 2 var word = "" switch number { case 1: word = "One" case 2: word = [1] default: word = "Unknown" } print(word)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a number instead of a string.
Forgetting the quotes around the word.
✗ Incorrect
The case for number 2 should assign the string "Two" to the variable word.
2fill in blank
mediumComplete the switch statement to print the correct message for the grade.
Swift
let grade = "B" switch grade { case "A": print("Excellent") case [1]: print("Good") default: print("Needs Improvement") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a character without quotes.
Using the wrong grade letter.
✗ Incorrect
The case should match the grade "B" to print "Good".
3fill in blank
hardFix the error in the switch statement by completing the missing case value.
Swift
let day = 3 switch day { case 1: print("Monday") case [1]: print("Tuesday") default: print("Other day") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for the case.
Using the wrong number for the case.
✗ Incorrect
The case should be 2 without quotes because day is an integer.
4fill in blank
hardFill both blanks to create a switch that prints the correct season for the month number.
Swift
let month = 4 switch month { case [1]: print("Winter") case [2]: print("Spring") default: print("Other season") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values instead of integers.
Mixing up the month numbers for seasons.
✗ Incorrect
Month 12 is winter, and month 4 is spring.
5fill in blank
hardFill all three blanks to create a switch that prints the day type based on the day number.
Swift
let dayNumber = 6 switch dayNumber { case [1]: print("Weekday") case [2]: print("Weekend") case [3]: print("Weekend") default: print("Invalid day") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning weekend to wrong day numbers.
Using strings instead of integers.
✗ Incorrect
Days 1 to 5 are weekdays, 6 and 7 are weekends.