JavaScript / Making decisions

Grouping cases and negation

Group switch cases that share a result, share things out in turn with the remainder, and flip a condition with !. Baccalaureate

Sometimes different values need the same action. In this program "sunny" and "cloudy" print the same sentence, so three lines are repeated for nothing:

JavaScript
const weather = "cloudy";

switch (weather) {
  case "sunny":
    console.log("Let's go shopping");
    break;
  case "cloudy":
    console.log("Let's go shopping");
    break;
  case "rainy":
    console.log("Let's stay home");
    break;
}
Console
Let's go shopping

The fix: write the cases one after another, then the action once. In a target game, hitting 4 or 8 wins a video game, 1 or 7 wins candy, anything else a sticker:

JavaScript
const target = 7;

switch (target) {
  case 4:
  case 8:
    console.log("Get video game");
    break;
  case 1:
  case 7:
    console.log("Get candy assortment");
    break;
  default:
    console.log("Get sticker");
    break;
}
Console
Get candy assortment
Grouping works because case 1: has no break, so the program runs on to the lines after case 7:. We used const instead of let because the target number never changes after it is hit; const declares a variable whose value cannot change.

Taking turns with the remainder

Students go into 4 groups in turn: the first in A, the second in B, the third in C, the fourth in D, then the fifth back in A. The remainder after dividing by 4 repeats in the same pattern, so 4 cases cover any number:

Student number12345678
num % 412301230
GroupABCDABCD
JavaScript
const num = 6;

switch (num % 4) {
  case 1:
    console.log("Student " + num + " is in Group A");
    break;
  case 2:
    console.log("Student " + num + " is in Group B");
    break;
  case 3:
    console.log("Student " + num + " is in Group C");
    break;
  case 0:
    console.log("Student " + num + " is in Group D");
    break;
}
Console
Student 6 is in Group B

Negation with !

Putting ! before a condition flips its result: true becomes false and false becomes true. Put the condition in brackets with ! in front, and it means "if it is not…":

JavaScript
console.log(5 > 3);
console.log(!(5 > 3));

let seat = "sold out";
if (!(seat == "available")) {
  console.log("It's sold out");
}
Console
true
false
It's sold out
Do not forget the brackets around the condition: !(seat == "available"). Here it gives the same result as seat != "available".

Practice

1Edit the programCurriculum practice

The prizes changed: target 8 also prints Video game Get, and target 7 also prints Candy Assortment Get. Add case 8: and case 7: by grouping them with the existing cases, without repeating console.log. With target = 7 it prints Candy Assortment Get.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const target = 7;

switch (target) {
  case 4:
    console.log("Video game Get");
    break;
  case 1:
    console.log("Candy Assortment Get");
    break;
  default:
    console.log("Sticker Get");
    break;
}
Console
Press Run to see the result.
2Write the programCurriculum practice

Write a switch on weather with grouped cases: "sunny" and "cloudy" print Let's go shopping, "rainy" prints Let's stay home.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const weather = "cloudy";
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Fill in the two blanks so the switch checks the remainder of num divided by 4, printing Student 7 is in Group C.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const num = 7;

___ (___) {
  case 0:
    console.log("Student " + num + " is in Group D");
    break;
  case 1:
    console.log("Student " + num + " is in Group A");
    break;
  case 2:
    console.log("Student " + num + " is in Group B");
    break;
  case 3:
    console.log("Student " + num + " is in Group C");
    break;
}
Console
Press Run to see the result.
4Edit the programCurriculum practice

Rewrite the condition on line 2 as the negation of seat == "available", so it prints It's sold out.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let seat = "sold out";
if (seat == "available") {
  console.log("It's sold out");
}
Console
Press Run to see the result.
5Write the programExtra practice

In Egypt the weekend is Friday and Saturday. Write a switch on day with grouped cases: "Friday" and "Saturday" print Weekend, "Sunday" to "Thursday" print School day, anything else prints Not a day.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const day = "Tuesday";
Console
Press Run to see the result.
6Write the programExtra practice

month is a month number (1 to 12). Print the season: 12, 1, 2 Winter; 3, 4, 5 Spring; 6, 7, 8 Summer; 9, 10, 11 Autumn.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const month = 4;
Console
Press Run to see the result.
7Write the programExtra practice

homework = "not done". Using !, print Finish your homework first if its value is not "done".

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let homework = "not done";
Console
Press Run to see the result.

Key points

  • To run the same action for different values, stack case 4: case 8: and write the action once.
  • switch (n % 4) shares numbers out between 4 groups in turn.
  • !(condition) flips the result: true becomes false and the other way round.