-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals_notes.html
More file actions
195 lines (139 loc) · 4.16 KB
/
conditionals_notes.html
File metadata and controls
195 lines (139 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Conditionals Lecture</title>
</head>
<body>
<h1>Conditionals Lecture</h1>
<script>
"use strict";
// ======================= IF STATEMENTS
/*
if (condition) {
action; // only runs if condition true
}
*/
var isAdmin = true;
if (isAdmin) {
alert('Displaying admin panel...');
}
// ======================= IF STATEMENT WITH ELSE
/*
if (condition) {
action; // runs if condition true
} else {
action; // runs if condition false
}
*/
var productInStock = false;
if (productInStock) {
alert('Display a purchase button');
} else {
alert('Display a message that says "OUT OF STOCK"');
}
// ======================= IF STATEMENTS WITH ELSE IF
/*
if (condition1) {
action; // runs if condition true
} else if (condition2) {
action; // runs if condition 1 is false and condition 2 is true
} else {
action; // runs if condition 1 and condition 2 are false
}
*/
var weather = 'rainy';
if(weather === 'sunny') {
alert('putting on sunglasses...');
} else if (weather === 'rainy') {
alert('put on a rain coat');
}else {
alert('I\'m going to stay inside');
}
// ======================= NESTED CONDITIONALS
var weatherCondition = 'sunny';
var temp = '';
if (weatherCondition === 'sunny') {
alert('Put on some sunglasses');
if (temp < 65) {
alert('Putting on jacket');
} else {
alert('Puting on fancy shirt');
}
} else if (weatherCondition === 'cloudy') {
alert('Don\'t put on sunglasses');
if (temp < 65) {
alert('Putting on a jacket');
} else {
alert('Putting on fancy shirt');
}
} else {
alert('Stay inside');
}
// ======================= TERNARY OPERATORS
// use when only one condition is being evaluated and may be only true or false
/*
(condition) ? returnValueIfTrue : returnValueIfFalse
*/
// If / Else
var someNumber = 9;
var divisibleByFive;
if (someNumber % 5 === 0) {
divisibleByFive = "Number is divisible by five.";
} else {
divisibleByFive = "Number is not divisible by five.";
}
alert(someNumber + " " + divisibleByFive);
// Refactor
// ======================= SWITCH STATEMENTS
// use a switch statement if a single condition may have multiple possible values
/*
switch(condition) {
case someOutput1:
...do something;
break;
case someOutput2:
...do something;
break;
(can continue to add cases)
default:
this will happen if no other case values match the switch condition value;
}
*/
var bondFilm = "Goldfinger";
if (bondFilm === "Dr. No") {
console.log("Fantastic!");
} else if (bondFilm === "From Russia With Love") {
console.log("Exploding barrels!");
} else if (bondFilm === "Goldfinger") {
console.log("\"I expect you to die, Mr. Bond.\"");
} else if (bondFilm === "Thunderball") {
console.log("Kinda long.");
} else if (bondFilm === "You Only Live Twice") {
console.log("One of the better theme songs.");
} else {
console.log("That's not one of the first five Bond films.");
}
var bondFilm = "Goldfinger";
switch(bondFilm) {
case "Dr. No":
console.log("Fantastic!");
break;
case "From Russia With Love":
console.log("Exploding barrels!");
break;
case "Goldfinger":
console.log("\"I expect you to die, Mr. Bond.\"");
break;
case "Thunderball":
console.log("Kinda long.");
case
case "You Only Live Twice":
console.log("One of the better theme songs.");
default:
console.log("That's not one of the first five Bond films.");
}
// refactor
</script>
</body>
</html>