Sometimes different values need the same action. In this program "sunny" and "cloudy" print the same sentence, so three lines are repeated for nothing:
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;
}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:
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;
}Get candy assortment
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 number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| num % 4 | 1 | 2 | 3 | 0 | 1 | 2 | 3 | 0 |
| Group | A | B | C | D | A | B | C | D |
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;
}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…":
console.log(5 > 3);
console.log(!(5 > 3));
let seat = "sold out";
if (!(seat == "available")) {
console.log("It's sold out");
}true false It's sold out
!(seat == "available"). Here it gives the same result as seat != "available".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.
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;
}Press Run to see the result. Write a switch on weather with grouped cases: "sunny" and "cloudy" print Let's go shopping, "rainy" prints Let's stay home.
const weather = "cloudy";
Press Run to see the result. Fill in the two blanks so the switch checks the remainder of num divided by 4, printing Student 7 is in Group C.
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;
}Press Run to see the result. Rewrite the condition on line 2 as the negation of seat == "available", so it prints It's sold out.
let seat = "sold out";
if (seat == "available") {
console.log("It's sold out");
}Press Run to see the result. 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.
const day = "Tuesday";
Press Run to see the result. 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.
const month = 4;
Press Run to see the result. homework = "not done". Using !, print Finish your homework first if its value is not "done".
let homework = "not done";
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.