Practice Qs Switch Statement to Determine the Day of the Week

Anmol singhAnmol singh
3 min read

Using the Switch Statement to Determine the Day of the Week

In this article, we will explore how to use a switch statement in JavaScript to determine the day of the week based on a number input. This is a great example to demonstrate how switch statements can simplify decision-making when dealing with fixed values.


Problem Statement

Write a program using a switch statement to print the name of the day corresponding to a number between 1 and 7. The mapping is as follows:

  • 1 → Monday

  • 2 → Tuesday

  • 3 → Wednesday

  • 4 → Thursday

  • 5 → Friday

  • 6 → Saturday

  • 7 → Sunday

If the input is not within this range, display an appropriate error message.


The Code Implementation

Here’s how the switch statement can be used to achieve the desired functionality:

let day = "6";

switch(day) {
    case "1":
        console.log("Monday");
        break;
    case "2":
        console.log("Tuesday");
        break;
    case "3":
        console.log("Wednesday");
        break;
    case "4":
        console.log("Thursday");
        break;
    case "5":
        console.log("Friday");
        break;
    case "6":
        console.log("Saturday");
        break;
    case "7":
        console.log("Sunday");
        break;
    default:
        console.log("Invalid day");
}

Understanding the Code

  1. Variable Declaration:
    A variable day is declared and assigned a value (in this case, "6").

  2. Switch Statement:

    • The switch statement evaluates the value of day.

    • Each case represents a possible value of day (e.g., "1" for Monday).

    • If a match is found, the corresponding block of code executes, printing the day name.

  3. Break Statement:

    • The break statement prevents the execution from "falling through" to the next case.
  4. Default Case:

    • The default block handles any input that does not match the predefined cases, ensuring the program is robust against invalid inputs.

Output

For the provided example, where day = "6", the output is:

Saturday

Enhancements and Additional Information

  1. Handling Numbers Directly
    In the current implementation, the day variable is a string. If you want to handle numeric input directly, you can modify the code to:

     let day = 6; // Now a number
    
     switch(day) {
         case 1:
             console.log("Monday");
             break;
         case 2:
             console.log("Tuesday");
             break;
         case 3:
             console.log("Wednesday");
             break;
         case 4:
             console.log("Thursday");
             break;
         case 5:
             console.log("Friday");
             break;
         case 6:
             console.log("Saturday");
             break;
         case 7:
             console.log("Sunday");
             break;
         default:
             console.log("Invalid day");
     }
    

    OUTPUT

    Saturday

  2. Improved Error Handling
    Add checks to ensure the input is within the expected range:

     javascriptCopyEditlet day = 8;
    
     if (day < 1 || day > 7) {
         console.log("Error: Please enter a number between 1 and 7.");
     } else {
         switch(day) {
             case 1: console.log("Monday"); break;
             case 2: console.log("Tuesday"); break;
             case 3: console.log("Wednesday"); break;
             case 4: console.log("Thursday"); break;
             case 5: console.log("Friday"); break;
             case 6: console.log("Saturday"); break;
             case 7: console.log("Sunday"); break;
         }
     }
    
  3. Real-World Application

    • This approach can be adapted for calendar or scheduling applications.

    • It can also serve as a learning tool for understanding days of the week in different languages.



0
Subscribe to my newsletter

Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anmol singh
Anmol singh