forked from girldevelopit/gdi-featured-js-intro
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathclass2.html
499 lines (457 loc) · 18.1 KB
/
class2.html
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 2 ~ JavaScript for Beginners ~ Girl Develop It</title>
<meta name="description" content="This is an introduction to JavaScript curriculum, developed by Sylvia Richardson for the Raleigh/Durham chapter.
The course is meant to be taught in 4 two-hour sections. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/gdidefault.css" id="theme">
<link rel="stylesheet" href="css/custom.css">
<!-- For syntax highlighting -->
<!-- light editor<link rel="stylesheet" href="lib/css/light.css">-->
<!-- dark editor--><link rel="stylesheet" href="reveal/lib/css/dark.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" alt="GDI logo">
<h3>JavaScript for Beginners</h3>
<h4>Class 2</h4>
</section>
<section>
<h3>Review</h3>
<p>In this code, spot the comments, variables, operator, function, argument, and return value.</p>
<pre><code>
function calculateTip(total) {
var tipPercent = 0.15; //Can be changed
return (total * tipPercent);
}
var billPreTip = 10;
var billTip = calculateTip(billPreTip);
var receipt = 'Meal: ' + billPreTip + ' Tip: ' + billTip +
' Total: ' + (billPreTip + billTip);
console.log(receipt);
</code></pre>
</section>
<section>
<h3>Variable Scope</h3>
<p>The scope of a variable is how long the computer will remember it.</p>
</section>
<section>
<h3>Global Scope</h3>
<p>A variable declared outside a function has a <span class="yellow">global</span> scope and can be accessed anywhere, even in a function.</p>
<pre><code>
var awesomeGroup = 'Girl Develop It'; //Global scope
function whatIsAwesome() {
console.log (awesomeGroup + ' is pretty awesome.'); //Will work
}
whatIsAwesome();
</code></pre>
</section>
<section>
<h3>Local Scope</h3>
<p>A variable declared within a function has a <span class="yellow">local</span> scope and can only be accessed within that function.</p>
<pre><code>
function whatIsAwesome() {
var awesomeGroup = 'Girl Develop It'; //Local scope
console.log ('I made a variable called awesomeGroup with a value of ' + awesomeGroup); //Will work
}
whatIsAwesome();
console.log (awesomeGroup + ' is pretty awesome.'); //Won't work
</code></pre>
</section>
<section data-markdown>
<h3>Block Scope</h3>
<p>"let" allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.
This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.</p>
<h5>Consider this example</h5>
```js
function varTest() {
var x = 1;
if (true) {
var x = 2; // same variable!
console.log(x); // 2
}
console.log(x); // 2
}
function letTest() {
let x = 1;
if (true) {
let x = 2; // different variable
console.log(x); // 2
}
console.log(x); // 1
}
```
</section>
<section>
<h3>Boolean Variables</h3>
<p>Boolean variables represent the logical values True and False</p>
<pre><code>
var catsAreBest = true;
var dogsRule = false;
</code></pre>
<p>If you try to use another variable as a boolean, JavaScript will guess. The number 0, the empty string '', undefined, and null are considered false, everything else reads as true.</p>
</section>
<section>
<h3>The if statement</h3>
<p>Use if to decide which lines of code to execute, based on a condition.</p>
<pre><code>
if (condition) {
// statements to execute
}
</code></pre>
<pre><code>
var bananas = 5;
if (bananas > 0) {
console.log ('You have some bananas!');
}
</code></pre>
</section>
<section>
<h3>Comparison Operators</h3>
<table class="smalltext">
<thead>
<tr>
<th width="150px">Example</th>
<th width="250px">Name</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>a == b</td>
<td>Equal</td>
<td><strong><code>TRUE</code></strong> if a is equal to b (can be different types).</td>
</tr>
<tr>
<td>a === b</td>
<td>Identical</td>
<td>
<strong><code>TRUE</code></strong> if a is equal to b, and the same type. </td>
</tr>
<tr>
<td>a != b</td>
<td>Not equal</td>
<td><strong><code>TRUE</code></strong> if a is not equal to b (can be different types).</td>
</tr>
<tr>
<td>a !== b</td>
<td>Not identical</td>
<td>
<strong><code>TRUE</code></strong> if a is not equal to b, or they are not the same type.</td>
</tr>
<tr>
<td>a < b</td>
<td>Less than</td>
<td><strong><code>TRUE</code></strong> if a is strictly less than b.</td>
</tr>
<tr>
<td>a > b</td>
<td>Greater than</td>
<td><strong><code>TRUE</code></strong> if a is strictly greater than b.</td>
</tr>
<tr>
<td>a <= b</td>
<td>Less than or equal to </td>
<td><strong><code>TRUE</code></strong> if a is less than or equal to b.</td>
</tr>
<tr>
<td>a >= b</td>
<td>Greater than or equal to </td>
<td><strong><code>TRUE</code></strong> if a is greater than or equal to b.</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3>Watch out!</h3>
<p>Don't mix up = and ==</p>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Make a variable called "temperature." Write some code that tells you to put on a coat if it is below 50 degrees.</p>
</section>
<section>
<h3>The if/else statement</h3>
<p>Use else to provide an alternate set of instructions.</p>
<pre><code>
var age = 28;
if (age >= 16) {
console.log ('Yay, you can drive!');
} else {
console.log ('Sorry, but you have ' + (16 - age) +
' years until you can drive.');
}
</code></pre>
</section>
<section>
<h3>The if/else if/else statement</h3>
<p>If you have multiple conditions, you can use else if.</p>
<pre><code>
var age = 20;
if (age >= 35) {
console.log('You can vote AND hold any place in government!');
} else if (age >= 25) {
console.log('You can vote AND run for the Senate!');
} else if (age >= 18) {
console.log('You can vote!');
} else {
console.log('You can\'t vote, but you can
still write your representatives.');
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Modify your "wear a coat" code for these conditions:</p>
<ol>
<li>If it is less than 50 degrees, wear a coat.</li>
<li>If it is less than 30 degrees, wear a coat and a hat.</li>
<li>If it is less than 0 degrees, stay inside.</li>
<li>Otherwise, wear whatever you want.</li>
</ol>
</section>
<section>
<h3>Logical Operators</h3>
<table>
<thead>
<tr>
<th width="200px">Example</th>
<th width="150px">Name</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>a && b</td>
<td>And</td>
<td><strong><code>TRUE</code></strong> if both a and b are <strong><code>TRUE</code></strong>.</td>
</tr>
<tr>
<td>a || b</td>
<td>Or</td>
<td><strong><code>TRUE</code></strong> if either a or b is <strong><code>TRUE</code></strong>.</td>
</tr>
<tr>
<td>! a</td>
<td>Not</td>
<td><strong><code>TRUE</code></strong> if a is not <strong><code>TRUE</code></strong>.</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3>Using logical operators</h3>
<p>You can use these operators to combine conditions.</p>
<pre><code>
var bananas = 5;
if (bananas >=2 && bananas <7) {
console.log ('You have a reasonable number of bananas');
} else {
console.log ('Check your banana supply');
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Add a logical operator to your what to wear program.</p>
</section>
<section>
<h3>While loops</h3>
<p>While will repeat the same code over and over until some condition is met.</p>
<pre><code>
var bottlesOfBeer = 99;
while (bottlesOfBeer >= 1) {
console.log (bottlesOfBeer + ' bottles of beer on the wall');
bottlesOfBeer = bottlesOfBeer - 9;
}
</code></pre>
</section>
<section>
<h3>Infinite Loops</h3>
<p>Make sure something changes in the loop, or your loop will go on forever...</p>
</section>
<section>
<h3>For loops</h3>
<p>For loops are very similar, but you declare a counter in the statement.</p>
<pre><code>
//will count 1 to 10
for (var i = 1; i <= 10; i++) {
console.log (i);
}
</code></pre>
</section>
<section data-markdown>
<h3>Remember talking about block scope earlier?</h3>
<p>Let's try some science</p>
<pre><code>
for (var i = 0; i < 10; i++) {
console.log(i);
}
console.log(i); /*What is the value of i??*/
for (let i = 0; i <10; i++) {
console.log(i);
}
console.log(i); /*What is the value of i??*/
</code></pre>
</section>
<section>
<h3>Loops and logic</h3>
<p>You can add other statements or logical operators inside the loops.</p>
<pre><code>
//Count from 1 to 50
for (var i = 1; i <= 50; i++) {
console.log (i);
//Says 'Buzz' after multiples of three
if (i % 3 == 0) {
console.log (' Buzz');
}
//Says 'Bang' after multiples of five
if (i % 5 == 0) {
console.log (' Bang');
}
}
</code></pre>
</section>
<section>
<h3>Break</h3>
<p>To exit a loop, use the break statement.</p>
<pre><code>
//Count from 100 to 200
for (var i = 100; i <= 200; i++) {
console.log('Testing ' + i);
//Stop at the first multiple of 7
if (i % 7 == 0) {
console.log('Found it! ' + i);
break;
}
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Write a loop that gives you the 9's times table,<br /> from 9 x 1 = 9 to 9 x 12 = 108.</p>
<p>Finish early? Try using a loop inside a loop to write all the times tables, from 1 to 12.</p>
</section>
<section>
<h3>Arrays</h3>
<p>Arrays are ordered lists of values.</p>
<pre><code>
var arrayName = [element0, element1, ...];
</code></pre>
<p>You can put different types of data into an array.</p>
<pre><code>
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
var lotteryNumbers = [33, 72, 64, 18, 17, 85];
var myFavoriteThings = ['Broccoli', 1024, 'Sherlock'];
</code></pre>
</section>
<section>
<h3>Array Length</h3>
<p>The length property tells you how many things are in an array</p>
<pre><code>
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
console.log(rainbowColors.length);
</code></pre>
</section>
<section>
<h3>Using Arrays</h3>
<p>You can access items with "bracket notation" by using the position of the object you want. Programmers start counting at zero.</p>
<pre><code>
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
var firstColor = rainbowColors[0];
var lastColor = rainbowColors[6];
</code></pre>
</section>
<section>
<h3>Changing arrays</h3>
<p>You can use bracket notation to change an item in an array</p>
<pre><code>
var myFavoriteThings = ['Broccoli', 1024, 'Sherlock'];
myFavoriteThings[0] = 'Asparagus';
console.log(myFavoriteThings); //What is logged to the console?
</code></pre>
</section>
<section>
<h3>Expanding arrays</h3>
<p>Arrays do not have a fixed length. You can use "push" to add something to an array.</p>
<pre><code>
var myFavoriteThings = ['Broccoli', 1024, 'Sherlock'];
myFavoriteThings.push('Dancing');
console.log(myFavoriteThings); //What is logged to the console?
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Create an array of your favorite foods. Log a few values in the console.</p>
</section>
<section>
<h3>Iterating through arrays</h3>
<p>Use a for loop to easily process each item in an array.</p>
<pre><code>
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'];
for (var i = 0; i < rainbowColors.length; i++) {
console.log(rainbowColors[i]);
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Use a loop to make a list of all your favorite foods.</p>
</section>
<section>
<h3>Resources</h3>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide" target="_blank">JavaScript Guide</a>, from the Mozilla Developers Network.</li>
<li><a href="http://www.codecademy.com/tracks/javascript" target="_blank">Code Academy</a>, with interactive JavaScript lessons to help you review.</li>
</ul>
</section>
</div><!-- Close .slides -->
<footer>
<div class="copyright">
JavaScript for Beginners ~ Girl Develop It ~
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div><!-- Close .copyright -->
</footer>
</div><!-- Close .reveal -->
<script src="reveal/lib/js/head.min.js"></script>
<script src="reveal/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
touch: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>