The browser storage localStorage is not available. Either your browser does not support it or you have disabled it or the maximum memory size is exceeded. Without localStorage your solutions will not be stored.

Switch

The switch statement allows you to execute different blocks of code based on the value of a variable. It's a cleaner alternative to writing multiple if...else if conditions when checking the same variable against different values. The comparison is made with strict equality (===).

The structure of a switch statement looks like this:
let fruit = 'apple';
switch (fruit) {
  case 'banana':
    console.log('Yellow');
    break;
  case 'apple':
    console.log('Red');
    break;
  default:
    console.log('unknown');
}
Each case compares the value of the variable (in this case, fruit) with a specific value (like 'banana' or 'apple'). If a match is found, the corresponding code block is executed.

The break statement tells the program to exit the switch after executing the matching case. If you omit break, the code of the next case is executed, even if its condition is not fulfilled. This can lead to confusing code, so it's best to end each case with break. In functions, you can also use return as an alternative.

If no case matches, the code in default will run (if provided). In the example above, the value of fruit is 'apple', so the output is Red.

Exercise

Write a function getDayName that takes a number between 1 and 7 and returns the corresponding weekday name. If no number between 1 and 7 is passed, 'unknown' should be returned.

Example: getDayName(1) should return 'Monday'.
function getDayName(n) {
  switch (n) {
    ...
  }
}
function getDayName(n) {
  switch (n) {
    case 1:
      return 'Monday';
    case 2:
      return 'Tuesday';
    case 3:
      return 'Wednesday';
    case 4:
      return 'Thursday';
    case 5:
      return 'Friday';
    case 6:
      return 'Saturday';
    case 7:
      return 'Sunday';
    default:
      return 'unknown';
  }
}

loving