Practice Qs Switch Statement to Determine the Day of the Week


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
Variable Declaration:
A variableday
is declared and assigned a value (in this case,"6"
).Switch Statement:
The switch statement evaluates the value of
day
.Each
case
represents a possible value ofday
(e.g.,"1"
for Monday).If a match is found, the corresponding block of code executes, printing the day name.
Break Statement:
- The
break
statement prevents the execution from "falling through" to the next case.
- The
Default Case:
- The
default
block handles any input that does not match the predefined cases, ensuring the program is robust against invalid inputs.
- The
Output
For the provided example, where day = "6"
, the output is:
Saturday
Enhancements and Additional Information
Handling Numbers Directly
In the current implementation, theday
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
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; } }
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.
Subscribe to my newsletter
Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
