Skip to main content

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. 1

    Switch on a day

    easy10 min

    Show "start" for Monday using a switch. The day is Monday.

  2. 2

    Use the default case

    easy10 min

    An unknown colour should fall through to "unknown".

  3. 3

    Group two cases

    easy10 min

    Both "Sat" and "Sun" should show "weekend". The day is Sun.

  4. 4

    Switch on a number

    easy10 min

    Show "two" when the value is 2.

  5. 5

    Return a label from a switch

    medium10 min

    Map the status code 404 to "not found".

Find the bug(5)

  1. 1

    Fix the missing break

    easy8 min

    Without a break the next case runs and overwrites the answer.

  2. 2

    Fix the strict type mismatch

    easy8 min

    switch compares strictly, so a string never matches a number case.

  3. 3

    Fix the missing default

    easy8 min

    An unmatched value leaves the output blank.

  4. 4

    Fix the wrong case value

    easy8 min

    The case tests the wrong day, so it falls to the default.

  5. 5

    Fix the case-sensitive match

    easy8 min

    The 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