0
0
Javascriptprogramming~15 mins

Switch statement in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a Switch Statement in JavaScript
📖 Scenario: You are creating a simple program that tells the day of the week based on a number input. This is like a small helper that converts numbers 1 to 7 into day names.
🎯 Goal: Build a JavaScript program that uses a switch statement to print the correct day name for a given number from 1 to 7.
📋 What You'll Learn
Create a variable called dayNumber with a number from 1 to 7
Create a variable called dayName to store the day name
Use a switch statement with dayNumber to assign the correct day name to dayName
Print the value of dayName
💡 Why This Matters
🌍 Real World
Switch statements are useful when you want to choose between many options based on one value, like menu selections or commands.
💼 Career
Understanding switch statements helps in writing clear and efficient code for decision-making in many programming jobs.
Progress0 / 4 steps
1
Create the dayNumber variable
Create a variable called dayNumber and set it to the number 3.
Javascript
Need a hint?

Use let dayNumber = 3; to create the variable.

2
Create the dayName variable
Create a variable called dayName and set it to an empty string "".
Javascript
Need a hint?

Use let dayName = ""; to create the variable.

3
Use a switch statement to assign the day name
Write a switch statement using dayNumber to assign the correct day name to dayName. Use these cases:
1: "Monday"
2: "Tuesday"
3: "Wednesday"
4: "Thursday"
5: "Friday"
6: "Saturday"
7: "Sunday"
Include a default case that sets dayName to "Invalid day".
Javascript
Need a hint?

Use switch (dayNumber) { case 1: dayName = "Monday"; break; ... default: dayName = "Invalid day"; }

4
Print the dayName
Write a console.log statement to print the value of dayName.
Javascript
Need a hint?

Use console.log(dayName); to print the day name.