I am trying to solve an exercise where I have to convert an if block to a switch. The command is
The objective of this exercise is to print a text on the console that will vary depending on the day that contains the day variable. For this, we have already presented a code made with if/else that we will have to modify and convert it to a switch, keeping the same result.
The original code is:
let dia = 'jueves'
function finDeSemana (dia) {
if (dia == 'viernes') {
console.log('buen finde')
} else if (dia == 'lunes') {
console.log('buena semana')
} else {
console.log('buen dia')
}
}
and this is what i do
let finDeSemana = dia
switch (dia) {
case 'viernes':
console.log('buen finde');
break;
case 'lunes':
console.log('buena semana');
break;
default:
console.log('buen dia')
}
Passing the if/else you've done well, but not what's out. The original code uses a function that you haven't implemented in the new code. "Weekend" which was the name of the function you have defined as a variable. And "day", which was a variable, you do not define it and that is why it gives you an error. The transcript would be:
In the second code day is not defined, Weekend is defined and then inside the Switch you are doing the conditional to "day". If that is the case, you would solve it by replacing line 1 of the second exercise with the first one, where you declare the constant. The other way is to pass "Weekend" to the switch and assign Weekend to X day as a string. Ex: