0
0
Javascriptprogramming~10 mins

Switch statement in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a switch statement that checks the variable color.

Javascript
switch([1]) {
  case 'red':
    console.log('Red color');
    break;
  default:
    console.log('Unknown color');
}
Drag options to blanks, or click blank then click option'
Ainput
Bvalue
Ccolor
Dchoice
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined or unrelated to the switch.
Leaving the parentheses empty.
2fill in blank
medium

Complete the code to add a case for the value 'blue' that logs 'Blue color'.

Javascript
switch(color) {
  case 'red':
    console.log('Red color');
    break;
  case [1]:
    console.log('Blue color');
    break;
  default:
    console.log('Unknown color');
}
Drag options to blanks, or click blank then click option'
A'green'
B'blue'
C'yellow'
D'black'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the case value.
Using a different color string than 'blue'.
3fill in blank
hard

Fix the error by completing the code to properly end the switch statement.

Javascript
switch(color) {
  case 'red':
    console.log('Red color');
    break;
  default:
    console.log('Unknown color');
  [1]
}
Drag options to blanks, or click blank then click option'
A}
Breturn
Cbreak;
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' which is not valid in JavaScript.
Using 'break;' outside a case block.
Using 'return' which is not the block end.
4fill in blank
hard

Fill both blanks to create a switch that logs 'Small' for 1, 'Medium' for 2, and 'Large' for 3.

Javascript
switch([1]) {
  case 1:
    console.log('Small');
    break;
  case [2]:
    console.log('Medium');
    break;
  case 3:
    console.log('Large');
    break;
  default:
    console.log('Unknown size');
}
Drag options to blanks, or click blank then click option'
Asize
B2
Clevel
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name in the switch expression.
Using a case value that does not match the intended number.
5fill in blank
hard

Fill both blanks to create a switch that logs the day name for 1, 2, and 3.

Javascript
switch([1]) {
  case 1:
    console.log([2]);
    break;
  case 2:
    console.log('Tuesday');
    break;
  case 3:
    console.log('Wednesday');
    break;
  default:
    console.log('Invalid day');
}
Drag options to blanks, or click blank then click option'
Aday
B'Monday'
D'Sunday'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name in the switch expression.
Not using quotes around the day name strings.