JavaScript switch Statement — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript switch Statement lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Switch on a day
easy10 minShow "start" for Monday using a switch. The day is Monday.
- 2
Use the default case
easy10 minAn unknown colour should fall through to "unknown".
- 3
Group two cases
easy10 minBoth "Sat" and "Sun" should show "weekend". The day is Sun.
- 4
Switch on a number
easy10 minShow "two" when the value is 2.
- 5
Return a label from a switch
medium10 minMap the status code 404 to "not found".
Find the bug(5)
- 1
Fix the missing break
easy8 minWithout a break the next case runs and overwrites the answer.
- 2
Fix the strict type mismatch
easy8 minswitch compares strictly, so a string never matches a number case.
- 3
Fix the missing default
easy8 minAn unmatched value leaves the output blank.
- 4
Fix the wrong case value
easy8 minThe case tests the wrong day, so it falls to the default.
- 5
Fix the case-sensitive match
easy8 minThe case uses different capitalisation, so nothing matches.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace