topskill.dev
CoursesTracks
0
All courses/Introduction to JavaScriptSwitch Statement
Control Flow
7 / 17
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking○Missing values and deliberate emptiness○Inspect Values in the Console○Inspect the Queue Report○Trace a Faulty Calculation○Repair the Score Calculation✦Module Quiz
○Declaring Variables○let and const in Practice○Operators○Type Conversion○Group and Convert Expense Inputs○Calculate the Event Expense Total○Track a Changing Points Tally○Adjust a Ticket Price✦Module Quiz
○Comparisons and strict equality○Truthiness and combined conditions○Function inputs and results○Conditionals○Combined Conditions Before FizzBuzz○Challenge: FizzBuzz Announcer●Switch Statement○Switch a Venue Sign○Challenge: Power Loadout○For Loops○While Loop○Break and Continue○Stop a Ticket Scan○Challenge: Number Guess Referee○Rainfall Routing Walkthrough○Score Batch Report✦Module Quiz
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions○Ticket Pricing Walkthrough○Shipping Labels with Defaults○Seat Ledger Closure Walkthrough○Private Budget Ledger✦Module Quiz
○Arrays○Student Scoreboard○Challenge: Student Course Scoreboard○Array Mutation Methods○Array Slice and Concat○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every○Charging Rack Restock Report○Pantry Restock Plan✦Module Quiz
○Object Key-Value Pairs○Student Course Record○Object Methods and Destructuring○Object copies and optional properties○Map and Set Collections○Playlist Lookup○Number and BigInt Boundaries○Ticket Ledger○JSON○Throwing and catching errors○Math and Date○Date Basics○Supply Bin Audit○Volunteer Shift Audit○Workshop Attendance Feed○Parcel Scan Feed✦Module Quiz
○String Methods○Template Literals○Regular Expressions○Build a Reminder with Template Literals○Format Pantry Pickup Messages○Validate a Time Slot with Regex○Validate Storage Shelf Labels✦Module Quiz
○Higher-Order Functions○Callbacks○map, filter, and reduce○Student Register with map○Challenge: Student Register○Recursion○Project - Data Transform Pipeline○Extend the inventory report○Handle an Error-First Callback○Report Seat Reservation Results○Summarize Completed Bike Rides○Summarize Delivered Parcels✦Module Quiz
○A notebook object○OOP Introduction○Independent workshop passes○Classes○An inherited notice○Inheritance○A private seat counter○Encapsulation○Equipment desk checkout○Project: Library Checkout✦Module Quiz
○Callback Timing○Asynchronous JavaScript○Promise Rehearsal○Promises and Async Await○Offline API Coordination○Async API Simulation○Partial Results Bulletin○Project: Resilient Awards Feed○Retry a Local Delivery○Project: Reliable Score Delivery✦Module Quiz

Switch Statement

For a day name, select one of three outcomes: weekend, weekday, or unknown.

The switch statement is an alternative to long if/else if chains when you are matching a single value against several discrete options.

Syntax

javascript
switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code when no case matched
}

When a case logs or assigns a result, use break to stop execution from falling into the next case. In a function, return also stops execution, so a case that returns does not need break.

Fall-through (sometimes intentional)

javascript
const light = "amber";
switch (light) {
    case "amber":
    case "red":
        console.log("Stop");
        break;
    default:
        console.log("Go");
}

Adjacent cases can share a result. The switch matches exact values: "Saturday" is not the same as "saturday".

The starter supplies switch (day) and all calls. Add each group of cases inside that switch. An empty case falls through to the next case, so Saturday can share Sunday's return without duplicating the result. A returned value ends the function; do not add a separate break after it.

Run after each group. The weekend line completes first, then the weekday line, then unknown names. Incomplete groups reach the undefined fallback and remain visible rather than stopping the script.

Edit script.js, Run to inspect the supplied probes, and Submit to check the tasks.

Your Task

  1. Add the Saturday and Sunday cases sharing the return value "Weekend". Both weekend probes should agree.
  2. Add Monday through Friday as cases sharing the return value "Weekday". All five weekday probes should agree.
  3. Make the default return "Unknown". The Holiday and lowercase saturday probes should both use it.
Hint 1
1 / 3
HINT 1

Each case matches the value after switch (expression).

HINT 2

A return exits the function, so returning cases do not need break.

HINT 3

Use default as the catch-all (like else).

Files
  • ⋄script.js
Loading editor…
Press Run to execute your code.