1switch(expression) {
2 case x:
3 // code block
4 break;
5 case y:
6 // code block
7 break;
8 default:
9 // code block
10}
1var gradeOut = 70;
2var totalGrade = 80;
3
4if (gradeOut >= 90)
5 gradeOut+= "My letter grade is: A";
6 else if (totalGrade < 90 && totalGrade >= 80)
7 gradeOut+= "My letter grade is: B";
8 else if (totalGrade < 80 && totalGrade >= 70)
9 gradeOut+= "My letter grade is: C";
10 else if (totalGrade < 70 && totalGrade >= 60)
11 gradeOut+= "My letter grade is: D";
12 else if (totalGrade < 60)
13 gradeOut+= "My letter grade is: F";
14
15//here's the same thing using switch(true). Requires var gradeOut (is currently at top of page
16switch(true) {
17case gradeOut<60:
18 gradeOut= "My letter grade is: F";
19 break;
20case gradeOut<70:
21 gradeOut= "My letter grade is: D";
22 break;
23case gradeOut<80:
24 gradeOut="My letter grade is: C";
25 break;
26}
27
1const randomNumber = Math.floor(Math.random() * 100);
2console.log(randomNumber);
3if (randomNumber < 20) {
4 console.log('randomNumber меньше 20');
5} else if (randomNumber > 50) {
6 console.log('randomNumber больше 50');
7} else {
8 console.log('randomNumber больше 20, и меньше 50')
9}
1 function alert_msg(){
2 var Name = document.getElementById('name').value
3
4 if (Name == ''){
5 alert("Must input name");}
6
7 else if (isNaN(Name)){
8 alert("Thank you " + Name + " for contacting us!");
9
10 }
11
12 else
13 alert("Name cannot be a number!");
14 }
1 switch (true) {
2 case bid < 100000:
3 currentIncrease = 1000;
4 break;
5 case type != 1 && bid - 1000 < 100000:
6 currentIncrease = 1000;
7 break;
8 case type != 1 && bid - 2000 < 1000000:
9 currentIncrease = 2000;
10 break;
11 case bid < 1000000:
12 currentIncrease = 2000;
13 break;
14 default:
15 currentIncrease = 10000;
16 }