dayNumber with the value 3dayNameIf to hold the day name using if statementsdayNameSwitch to hold the day name using a switch statementdayNameIf and dayNameSwitch to compare resultsdayNumber with the value 3dayNameIf to hold the day name using if statementsdayNameSwitch to hold the day name using a switch statementdayNameIf and dayNameSwitch to compare resultsdayNumber variabledayNumber and set it to the number 3.Use const dayNumber = 3; to create the variable.
dayNameIf using if statementsdayNameIf. Use if, else if, and else statements to set dayNameIf to the correct day name for dayNumber. Use these mappings: 1 = 'Monday', 2 = 'Tuesday', 3 = 'Wednesday', 4 = 'Thursday', 5 = 'Friday', 6 = 'Saturday', 7 = 'Sunday'. If the number is not 1 to 7, set dayNameIf to 'Invalid day'.Use if and else if to check dayNumber and assign the correct day name to dayNameIf.
dayNameSwitch using a switch statementdayNameSwitch. Use a switch statement on dayNumber to set dayNameSwitch to the correct day name using the same mappings as before. Use a default case to set dayNameSwitch to 'Invalid day' if the number is not 1 to 7.Use switch (dayNumber) with case for each day number and a default for invalid numbers.
dayNameIf and dayNameSwitchconsole.log statements to print the values of dayNameIf and dayNameSwitch.Use console.log(dayNameIf); and console.log(dayNameSwitch); to print the results.
Which statement best describes when to use switch instead of if in JavaScript?
switchswitch is designed to compare one value against many possible fixed cases.if usageif is better for complex or varied conditions, like ranges or multiple variables.switch when checking one value against many fixed options. -> Option Cswitch = fixed value checks [OK]Which of the following is the correct syntax to start a switch statement in JavaScript?
?
switch syntaxswitch (variable) {.What will be the output of the following code?
const fruit = 'apple';
switch (fruit) {
case 'banana':
console.log('Yellow');
break;
case 'apple':
console.log('Red');
break;
default:
console.log('Unknown');
}fruitfruit is set to 'apple'.fruit in the switch casescase 'apple' matches, so it prints 'Red' and then breaks.Find the error in this switch statement and choose the fix:
const day = 3;
switch day {
case 1:
console.log('Monday');
break;
case 3:
console.log('Wednesday');
break;
default:
console.log('Other day');
}switch (day) {.day in switch: switch (day) { -> Option AYou want to write a program that prints a message based on a score:
Which approach is best?
switch is not suitable.if and else if statements can check ranges easily and clearly.if and else if statements to check ranges. -> Option DPass the test and this pattern moves unknown