-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.html
More file actions
320 lines (283 loc) · 9.48 KB
/
Copy pathjavascript.html
File metadata and controls
320 lines (283 loc) · 9.48 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Muhammad Abu Zer Portfolio</title>
<link rel="icon" type="image/x-icon" href="IT ADMIN-Abu.jpeg">
<link rel="stylesheet" href="styles/new.css">
<!-- <link rel="stylesheet" href="new.css"> -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700;800&display=swap" rel="stylesheet">
</head>
<body class="body-ui container ">
<ul class="navbar">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="#">More JS</a>
<ul class="submenu">
<li><a href="defenSiveProgramming.html" target="_blank">DefensiveProgramming</a></li>
<li><a href="#">Mission</a></li>
<li><a href="#">Vision</a></li>
</ul>
</li>
<li>
<a href="#">Services</a>
<ul class="submenu">
<li><a href="#">Web Design</a></li>
<li><a href="#">Graphic Design</a></li>
<li><a href="#">Digital Marketing</a></li>
</ul>
</li>
<li>
<a href="#">Portfolio</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
<h1>
Working With Conditional statements
</h1>
<article>
In this reading, you will learn when to use the if else statement and when to use the switch statement.
Both if else and switch are used to determine the program execution flow based on whether or not some conditions have been met.
This is why they are sometimes referred to as flow control statements. In other words, they control the flow of execution of your code, so that some code can be skipped, while other code can be executed.
At the heart of both flow control structures lies the evaluation of one or more conditions.
Generally, if else is better suited if there is a binary choice in the condition.
For example, in plain English: if it's sunny, wear sunglasses. Otherwise, don't.
In this case, using an if statement is an obvious choice.
When there are a smaller number of possible outcomes of truthy checks, it is still possible to use an if else statement, such as:
</article>
<a href="functions.html" target="_blank">
want to learn about Functions
</a>
<a href="mathobject.html" target="_blank">
Math Random Cheat sheet
</a>
<h2>
Copy these code and run in the console and ENJOY!
</h2>
<strong>
IT IS TO BE NOTED THAT "=" and "= =" are totally <span class="red">different</span> things in JavaScript "=" assigns a value whereas ""= ="" loosely comparision operator.
</strong>
<code> <pre>var light = "green";
if(light == "green") {
console.log("Drive")
} else if (light == "orange") {
console.log("Get ready")
} else if (light == "red") {
console.log("Dont' drive")
} else {
//this block will run if no condition matches
console.log("The car is not green, orange, or red");
}
</pre></code>
<h3> Using switch statements </h3>
<article>
However, if there are a lot of possible outcomes, it is best practice to use a switch statement because it is easier less verbose. Being easier to read, it is easier to follow the logic, and thus reduce cognitive load of reading multiple conditions.
Nevertheless, this is not a rule set in stone. It is simply a stylistic choice.
To reinforce this point, here's an example of the earlier if else conditional statement, using the switch syntax:
</article>
<code>
<pre>
//converting the previous if-else example with switch-case
var light = "green";
switch(light) {
case 'green':
console.log("Drive");
break;
case 'orange':
console.log("Get ready");
break;
case 'red':
console.log("Don't drive");
break;
default:
//this block will run if no condition matches
console.log('The light is not green, orange, or red');
break;
}
</pre>
</code>
<strong>
More examples of switch </strong>
<ol>
<li>
Code the days of the week program as a switch statement
</li>
<li>
On the next line, define a new variable, name it day, and set its value to "Sunday".
</li>
<li>
Start coding a switch statement, passing the day variable as the expression to evaluate.
</li>
<li>
Inside the switch, add cases for every day of the week, starting with 'Monday', and ending with 'Sunday'. Make sure to use string values for days. Inside each case, for now, just add a console.log('Do something'), and add a break; on the line below.
</li>
<li>
At the very bottom of the switch statement, add the default case and add a console.log('There is no such day').
</li>
<li>
Finally, update the console.log calls for each case, based on whatever activity you have on each of the days.
</li>
</ol>
</h3>
<code>
<pre>
<strong>
// Tips
// If you need to make sure that multiple conditions are true in an if statement, you can do so using the && operator
// In JavaScript, the correct syntax of the "greater than or equal to" operator is: >=.
// Don't forget to add a break at the very end of each case in a switch statement.
</strong>
<h3>
Lets Begin Copy and paste the following snippet see the result
</h3>
<code>
var day = `Sunday`;
switch(day) {
case 'Monday':
console.log('Read a book');
break;
case 'Tuesday':
console.log('Watch a movie');
break;
case 'Wednesday':
console.log('Read a book');
break;
case 'Thursday':
console.log('Play basketball');
break;
case 'Friday':
console.log('Socialize');
break;
case 'Saturday':
console.log('Chill');
break;
case 'Sunday':
console.log('Have barbecue');
break;
default:
//this block will run if no condition matches
console.log('There is no such day');
}
</code>
<strong> output - Have barbecue</strong>
</pre>
<h1>
for and while loops
</h1>
<strong>
Examples of incrementing a value using a for loop
</strong>
<p> Exercise Question 1:
Write a "for" loop that will perform exactly the same repetitive code as this:
</p>
<code>
console.log(5) <br>
console.log(4) <br>
console.log(3) <br>
console.log(2) <br>
console.log(1) <br>
console.log('Countdown finished!')
</code>
<strong>
OUTPUT:
</strong>
<code>
5 <br>
4 <br>
3 <br>
2 <br>
1 <br>
Countdown finished! <br>
</code>
<strong>
Solution: 1
</strong>
<code>
for (var i = 5; i > 0; i--) {
console.log(i);
};
console.log('Countdown finished!');
</code>
<hr>
<strong>
Note : that the syntax for Decrementing a number doesnt contain i < = 0. It will always be i > 0; and the i will start from the greter number like if want a countdown from 10
to 5 i will be "i = 10; i> 0 ;i-- " . Also No Semicolon "; " in the last i </strong>
<hr>
<h1>
While loops
</h1>
<strong>
Example 2 : Write a "while" loop that will perform exactly the same repetitive code as this:
</strong>
<code>
console.log(5) <br>
console.log(4)<br>
console.log(3)<br>
console.log(2)<br>
console.log(1)<br>
console.log('Countdown finished!')<br>
</code>
<h3>
OUTPUT :
</h3>
<code>
5 <br>
4 <br>
3 <br>
2 <br>
1 <br>
Countdown finished! <br>
</code>
<h3>
Solution :
</h3>
<strong>
we should not contain a semicolon in WHILE loop under the parenthesis <span class="red"> while (i > 0;)</span>
<span.bold>
CORRECT - while (i > 0)
</span.bold>
</strong>
<code>
var i = 5;
while (i > 0) {
console.log(i);
i = i - 1;
};
console.log('Counting completed!');
<br>
OUTPUT <br>
5 <br>
4 <br>
3 <br>
2 <br>
1 <br>
Counting completed!
</code>
<h3>
Nested Lopps
</h3>
<strong>
just Copy the code and paste it in the comsole and see the Game
</strong>
<code>
//nested loops - one inside another
for (var firstNum = 0; firstNum < 2; firstNum++) {
for (var secondNum = 0; secondNum < 10; secondNum++) {
console.log(firstNum + " times " + secondNum + " equals " + firstNum * secondNum);
}
}
</code>
<footer>
more Learnings <a href="http://tryCatch.htnl" target="_blank" rel="noopener noreferrer"> Error handling </a>
</footer>
</body>
</html>