0
0
Javascriptprogramming~10 mins

Switch vs if comparison in Javascript - Interactive Practice

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

Complete the code to check the value of color using an if statement.

Javascript
if (color [1] 'red') {
  console.log('Color is red');
}
Drag options to blanks, or click blank then click option'
A===
B!=
C=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of === causes assignment instead of comparison.
Using == can cause unexpected type coercion.
2fill in blank
medium

Complete the switch statement to handle the case when day is 'Monday'.

Javascript
switch(day) {
  case [1]:
    console.log('Start of the work week');
    break;
  default:
    console.log('Another day');
}
Drag options to blanks, or click blank then click option'
A'Sunday'
B'Monday'
C'Friday'
D'Saturday'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string case labels.
Using the wrong day string.
3fill in blank
hard

Fix the error in the if-else statement to correctly check if score is greater than 50.

Javascript
if (score [1] 50) {
  console.log('Passed');
} else {
  console.log('Failed');
}
Drag options to blanks, or click blank then click option'
A==
B<
C=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of > causes assignment, not comparison.
Using < checks the wrong condition.
4fill in blank
hard

Fill both blanks to create a switch statement that logs 'Weekend' for Saturday and Sunday.

Javascript
switch(day) {
  case [1]:
  case [2]:
    console.log('Weekend');
    break;
  default:
    console.log('Weekday');
}
Drag options to blanks, or click blank then click option'
A'Saturday'
B'Monday'
C'Sunday'
D'Friday'
Attempts:
3 left
💡 Hint
Common Mistakes
Using weekdays instead of weekend days.
Forgetting to include both weekend days.
5fill in blank
hard

Fill all three blanks to create an if-else chain that logs the correct message for fruit.

Javascript
if (fruit [1] 'apple') {
  console.log('Apple pie');
} else if (fruit [2] 'banana') {
  console.log('Banana smoothie');
} else {
  console.log([3]);
}
Drag options to blanks, or click blank then click option'
A===
C'Unknown fruit'
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of === can cause unexpected results.
Not using quotes around the string in the else block.