You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This example uses the weekday number to calculate the weekday name:
36
36
37
37
switch(newDate().getDay()){
38
-
case0:
39
-
day="Sunday";
40
-
break;
41
-
case1:
42
-
day="Monday";
43
-
break;
44
-
case2:
45
-
day="Tuesday";
46
-
break;
47
-
case3:
48
-
day="Wednesday";
49
-
break;
50
-
case4:
51
-
day="Thursday";
52
-
break;
53
-
case5:
54
-
day="Friday";
55
-
break;
56
-
case6:
57
-
day="Saturday";
38
+
case0:
39
+
day='Sunday';
40
+
break;
41
+
case1:
42
+
day='Monday';
43
+
break;
44
+
case2:
45
+
day='Tuesday';
46
+
break;
47
+
case3:
48
+
day='Wednesday';
49
+
break;
50
+
case4:
51
+
day='Thursday';
52
+
break;
53
+
case5:
54
+
day='Friday';
55
+
break;
56
+
case6:
57
+
day='Saturday';
58
58
}
59
59
60
60
/**
@@ -81,14 +81,14 @@ case 6:
81
81
// If today is neither Saturday (6) nor Sunday (0), write a default message:
82
82
83
83
switch(newDate().getDay()){
84
-
case6:
85
-
text="Today is Saturday";
86
-
break;
87
-
case0:
88
-
text="Today is Sunday";
89
-
break;
90
-
default:
91
-
text="Looking forward to the Weekend";
84
+
case6:
85
+
text='Today is Saturday';
86
+
break;
87
+
case0:
88
+
text='Today is Sunday';
89
+
break;
90
+
default:
91
+
text='Looking forward to the Weekend';
92
92
}
93
93
94
94
// The result of text will be:
@@ -99,14 +99,14 @@ default:
99
99
// Example
100
100
101
101
switch(newDate().getDay()){
102
-
default:
103
-
text="Looking forward to the Weekend";
104
-
break;
105
-
case6:
106
-
text="Today is Saturday";
107
-
break;
108
-
case0:
109
-
text="Today is Sunday";
102
+
default:
103
+
text='Looking forward to the Weekend';
104
+
break;
105
+
case6:
106
+
text='Today is Saturday';
107
+
break;
108
+
case0:
109
+
text='Today is Sunday';
110
110
}
111
111
112
112
// If default is not the last case in the switch block, remember to end the default case with a break.
@@ -119,16 +119,16 @@ case 0:
119
119
// Example
120
120
121
121
switch(newDate().getDay()){
122
-
case4:
123
-
case5:
124
-
text="Soon it is Weekend";
125
-
break;
126
-
case0:
127
-
case6:
128
-
text="It is Weekend";
129
-
break;
130
-
default:
131
-
text="Looking forward to the Weekend";
122
+
case4:
123
+
case5:
124
+
text='Soon it is Weekend';
125
+
break;
126
+
case0:
127
+
case6:
128
+
text='It is Weekend';
129
+
break;
130
+
default:
131
+
text='Looking forward to the Weekend';
132
132
}
133
133
134
134
// Switching Details
@@ -147,29 +147,63 @@ default:
147
147
148
148
// In this example there will be no match for x:
149
149
150
-
Example:
151
-
varx="0";
150
+
Example: varx='0';
152
151
switch(x){
153
-
case0:
154
-
text="Off";
155
-
break;
156
-
case1:
157
-
text="On";
158
-
break;
159
-
default:
160
-
text="No value found";
152
+
case0:
153
+
text='Off';
154
+
break;
155
+
case1:
156
+
text='On';
157
+
break;
158
+
default:
159
+
text='No value found';
161
160
}
162
161
163
162
/** Exercise for You Complete it */
164
163
165
164
// Create a switch statement that will alert "Hello" if fruits is "banana", and "Welcome" if fruits is "apple".
166
165
167
166
switch(fruits){
168
-
case"Banana":
169
-
alert("Hello");
170
-
break;
171
-
172
-
case"Apple":
173
-
alert("Welcome");
174
-
break;
175
-
}
167
+
case'Banana':
168
+
alert('Hello');
169
+
break;
170
+
171
+
case'Apple':
172
+
alert('Welcome');
173
+
break;
174
+
}
175
+
176
+
// Create a switch statement that will tell you what to wear based on the temperature outside. For temps below 50 degrees Fahrenheit, alert "Wear a coat." For temps between 51 and 70 degrees, alert "Bring a sweater." For temps above 70, alert "It should be warm."
177
+
178
+
vartemp=49;
179
+
180
+
switch(true){
181
+
casetemp<=50:
182
+
'Wear a coat.';
183
+
break;
184
+
casetemp>50&&temp<=70:
185
+
'Bring a sweater.';
186
+
break;
187
+
casetemp>70:
188
+
'It should be warm.';
189
+
}
190
+
191
+
// Create a switch statement to determine who wins in a game of rocks, paper, scissors.
0 commit comments