top of page

Switch : Exercise: Code the days of the week program as a switch statement

  • Writer: satyanarayan behera
    satyanarayan behera
  • Aug 25, 2022
  • 1 min read
  1. On the next line, define a new variable, name it day, and set its value to "Sunday".

  2. Start coding a switch statement, passing the day variable as the expression to evaluate.

  3. Inside the switch, add cases for every day of the week, starting with 'Monday', and ending with 'Sunday'. Make sure to use string values for days. Inside each case, for now, just add a console.log('Do something'), and add a break; on the line below.

  4. At the very bottom of the switch statement, add the default case and add a console.log('There is no such day').

  5. Finally, update the console.log calls for each case, based on whatever activity you have on each of the days.



var day = "Sunday";

switch(day) {

case 'Sunday':

console.log("Do something");

break;

case 'Monday':

console.log("Do something");

break;

case 'Tuseday':

console.log("Do something");

break;

case 'Wedneshday':

console.log("Do something");

break;

default:

console.log('There is no such day');

break;

}


 
 
 

Recent Posts

See All
For Loop : Task 1

Write a "for" loop that will perform exactly the same repetitive code as this: console.log(1) console.log(2) console.log(3)...

 
 
 

Comments


  • alt.text.label.LinkedIn

©2022 by Satya

bottom of page