forked from dytcodetogether/gdi-intermediate-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class1.html
573 lines (477 loc) · 18.1 KB
/
class1.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intermediate JavaScript</title>
<meta name="description" content="Girl Develop It framework for easily creating beautiful presentations using HTML in GDI theme. Forked from Hakim El Hattab's reveal.js">
<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="css/reveal.css">
<link rel="stylesheet" href="css/theme/simple.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor --><link rel="stylesheet" href="lib/css/light.css">
<!-- dark editor <link rel="stylesheet" href="lib/css/dark.css">-->
<!-- <link rel="stylesheet" href="lib/css/zenburn.css"> -->
<link rel="stylesheet" href="plugin/accessibility-helper/css/accessibility-helper.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="css/print/pdf.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<!--[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 -->
<section>
<img src="img/gdi-logo.png" alt="GDI Logo" class="noborder" style="width:8em;"/>
<h1>Intermediate JavaScript</h1>
<h2>Class 1: Variables, Arrays, and Objects</h2>
<p>
<small>View the slides at <a href ="https://github.com/gdiseattle/gdi-intermediate-js">github.com/gdiseattle/gdi-intermediate-js</a>.</small>
</p>
</section>
<section>
<h3>Let's Develop It</h3>
<ul>
<li>Go to <a href="https://codepen.io">https://codepen.io</a></li>
<li>Create a new pen</li>
<li>Write this code inside the JavaScript window.</li>
</ul>
<pre><code>
alert('Hello World!');
console.log('Secret message');
</code></pre>
</section>
<!-- Variables & Scope -->
<section>
<h3>Variables</h3>
<p>A variable is a place to store values</p>
<p>Review:</p>
<ul>
<li>Declaring variables</li>
<li>Initializing variables</li>
<li>What variables can contain</li>
<li>Naming rules for variables</li>
</ul>
</section>
<section>
<h3>Scope</h3>
<p>The scope of a variable is how long the computer will remember it.</p>
</section>
<section>
<h3>Local Scope</h3>
<pre><code class ="javascript">function func() {
var local = true;
console.log(local);
};
func();
console.log(local);</code></pre>
<a href="http://bit.ly/intermediate-js-local-scope" target=blank>http://bit.ly/intermediate-js-local-scope</a>
</section>
<section>
<h3>Global Scope</h3>
<pre><code class ="javascript">var global = "I'm available everywhere!";
function func() {
var local = "I'm available inside this function.";
console.log(local, global);
};
func();
console.log(global);</code></pre>
<a href="http://bit.ly/intermediate-js-global-scope" target=blank>http://bit.ly/intermediate-js-global-scope</a>
</section>
<section>
<h3>Sneaky global scope</h3>
<p>Be careful to properly declare and initialize your variables to avoid unexpected global scope.</p>
<pre><code class="javascript">var global = "I'm available everywhere.";
function func() {
local = "I'm available everywhere, too!";
};
func();
console.log(local);</code></pre>
</section>
<section>
<h3>Precedence</h3>
<pre><code class="javascript">var g = "global";
function go() {
var l = "local";
var g = "in here!";
console.log(g + " inside go()");
}
go();
alert(g + " outside go()");</code></pre>
<a href="http://bit.ly/intermediate-js-precedence" target=blank>http://bit.ly/intermediate-js-precedence</a>
</section>
<section>
<h2>Questions?</h2>
<p>Questions about variables, scope, or precedence?</p>
</section>
<!-- Arrays -->
<section>
<h3>Arrays</h3>
<pre><code class="javascript">var coolLadyCoders =
[
'Grace Hopper',
'Ada Lovelace',
'Dorothy Vaughan',
'Margaret Hamilton',
];</code></pre>
<a href="http://bit.ly/intermediate-js-coolLadyCoders" target=blank>http://bit.ly/intermediate-js-coolLadyCoders</a>
<ul>
<br>
<li class="fragment">How long is the array?</li>
<li class="fragment">What is the index of <strong>'Dorothy Vaughan'</strong>?</code></li>
<li class="fragment">What is at the 0th index?</li>
<li class="fragment">How do we find the length of an array?</li>
<li class="fragment">When would we use an array?</li>
</ul>
</section>
<section>
<h3>Assignment and expanding arrays</h3>
<pre><code class="javascript">var coolLadyCoders =
[
'Grace Hopper',
'Ada Lovelace',
'Dorothy Vaughan',
'Margaret Hamilton',
];
coolLadyCoders[0] = 'Ada Lovelace';
coolLadyCoders[1] = 'Grace Hopper';
coolLadyCoders.push('Anita Borg');
</code></pre>
<p>We can also use the <code>new</code> keyword to create a new array:</p>
<pre><code class="javascript">var coolLadyCoders = new Array();</code></pre>
</section>
<section>
<h3>Accessing items</h3>
<pre><code class="javascript">var coolLadyCoders =
[
'Grace Hopper',
'Ada Lovelace',
'Dorothy Vaughan',
'Margaret Hamilton',
];
var firstLady = coolLadyCoders[0];
var lastLady = coolLadyCoders[3];</code></pre>
</section>
<section>
<h3>Removing items</h3>
<p>The <code>pop()</code> method removes the last item from the array and returns it</p>
<pre><code class="javascript">var coolLadyCoders =
[
'Grace Hopper',
'Ada Lovelace',
'Dorothy Vaughan',
'Margaret Hamilton',
];
console.log(coolLadyCoders);
var removed = coolLadyCoders.pop();
console.log(coolLadyCoders);
console.log(removed);</code></pre>
</section>
<section>
<h3>Array review</h3>
<pre><code class="javascript">var coolLadyCoders =
[
'Grace Hopper',
'Ada Lovelace',
'Dorothy Vaughan',
'Margaret Hamilton'
];
var i = 0;
console.log(coolLadyCoders[i]); // What will this display?
console.log(coolLadyCoders[1]; // What will this display?
console.log(coolLadyCoders.pop()); // What will this display?
console.log(coolLadyCoders.length); // what will this display?
</code></pre>
</section>
<section>
<h3>Iterating over arrays</h3>
<pre><code class="javascript">for(var i = 0; i < coolLadyCoders.length; i++) {
console.log(coolLadyCoders[i]); // What will this display?
}
</code></pre>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Starting with this array:</p>
<pre><code class="javascript">var noisesArray = ["quack", "sneeze", "boom"];</code></pre>
<p>Output “<code>quack! sneeze! boom!</code>” to the console.</p>
</section>
<section>
<h3>The <code>join()</code> method of arrays</h3>
<p>Converts an array to a string.</p>
<p>Takes a single argument - the string that should separate the individual items.</p>
</section>
<section>
<h3><code>array.join()</code></h3>
<pre><code class="javascript">
console.log(coolLadyCoders.join());
console.log(coolLadyCoders.join(', '));
console.log(coolLadyCoders.join(' + '));
console.log(coolLadyCoders.join(''));</code></pre>
</section>
<section>
<h3>Let's Develop It! (again)</h3>
<p>Starting with this array:</p>
<pre><code class="javascript">var noisesArray = ["quack", "sneeze", "boom"];</code></pre>
<p>Output “<code>quack! sneeze! boom!</code>” to the console.</p>
</section>
<section>
<h3>The <code>filter()</code> method of arrays</h3>
<pre><code class="javascript">var numbers = [12, 5, 8, 44, 53, 130, 44, 4];
function isBigEnough(element) {
return element >= 10;
}
var bigNumbers = numbers.filter(isBigEnough);
console.log(numbers);
console.log(bigNumbers);</code></pre>
<p>The <code>filter()</code> method creates a new array with all elements that pass the test implemented by the provided function.</p>
<br>
<a href="http://bit.ly/intermediate-js-filter" target=blank>http://bit.ly/intermediate-js-filter</a>
</section>
<section>
<h3><code>filter()</code></h3>
<p>The <code>filter()</code> method requires one argument - a callback function that returns a Boolean</p>
<p>If the function returns true, the item is added to the new array.</p>
<pre><code class="javascript">var numbers = [12, 5, 8, 44, 53, 130, 44, 4];
function isBigEnough(element) {
return element >= 10;
}
var bigNumbers = numbers.filter(isBigEnough);</code></pre>
<p>The <code>filter()</code> callback function can take up to three arguments. They are: <code>element</code>, <code>index</code>, and <code>array</code></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Given this array:</p>
<pre><code class="javascript">var cats = [
{ name: "Maru", status: "hungry" },
{ name: "Hana", status: "playful" },
{ name: "Lil Bub", status: "sleepy" },
{ name: "Grumpy Cat", status: "hungry" }
];</code></pre>
<p>Filter the array to create a new array of just the cats who are hungry.</p>
<br>
<a href="http://bit.ly/intermediate-js-cats" target=blank>http://bit.ly/intermediate-js-cats</a>
</section>
<section>
<h3>The <code>map()</code> method of arrays</h3>
<p>The <code>map()</code> method creates a new array with the results of calling a function on each item in the array.</p>
</section>
<section>
<h3><code>map()</code></h3>
<p>The <code>map()</code> method requires one argument - a callback function that takes an item from the current array and returns an item that will be included in the new array.</p>
<pre><code class="javascript">var numbers = [57, 12, 13, 5, 143, 8, 17, 43];
var numbersPlusFive = numbers.map(function(num){
return num + 5;
});
console.log(numbers);
console.log(numbersPlusFive);</code></pre>
<p>The <code>map()</code> callback function can take up to three arguments. They are: <code>element</code>, <code>index</code>, and <code>array</code></p>
<br>
<a href="http://bit.ly/intermediate-js-map" target=blank>http://bit.ly/intermediate-js-map</a>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Given this array:</p>
<pre><code class="javascript">var cats = [
{ name: "Maru", status: "hungry" },
{ name: "Hana", status: "playful" },
{ name: "Lil Bub", status: "sleepy" },
{ name: "Grumpy Cat", status: "hungry" }
];</code></pre>
<p>Use the <code>map()</code> method to create a new array of strings representing the cats names. Like this:</p>
<pre><code class="javascript">var catNames = ["Maru", "Hana", "Lil Bub", "Grumpy Cat"];</code></pre>
</section>
<section>
<h2>Questions?</h2>
<p>Questions about <code>join()</code>, <code>filter()</code>, or <code>map()</code> before we move on?</p>
</section>
<!-- Objects -->
<section>
<h3>Objects Review</h3>
<pre><code class="javascript">var ollieTheCat = {
furColor: 'gray',
age: 2,
personality: 'wild',
likes: ['catnip', 'food', 'naps', 'cuddles', 'attention'],
adopted: {
month: 'february',
day: '11',
year: '2015'
}
};</code></pre>
<a href="http://bit.ly/intermediate-js-ollie" target=blank>http://bit.ly/intermediate-js-ollie</a>
<ul>
<br>
<li class="fragment">How many keys are in the object?</li>
<li class="fragment">What are the key names?</li>
<li class="fragment">What are the values?</li>
</ul>
</section>
<section>
<h3>Creating objects</h3>
<p>So far, we've learned how to create an object by doing this:</p>
<pre><code class="javascript">var ollieTheCat = {};</code></pre>
<p>Or by doing this:</p>
<pre><code class="javascript">var ollieTheCat = {
furColor: 'gray',
age: 1
};</code></pre>
<p>These are called <span class="blue">object literals</span> - a pair of curly braces with 0 or more property/value pairs.</p>
</section>
<section>
<h3>The <code>new</code> keyword</h3>
<p>We can also create new objects by using the <code>new</code> keyword, like this:</p>
<pre><code class="javascript">var ollieTheCat = new Object();</code></pre>
</section>
<section>
<h3>Assigning properties</h3>
<p>We can assign properties inside an object literal:</p>
<pre><code class="javascript">var ollieTheCat = {
furColor: 'gray'
};</code></pre>
<p>or by using dot notation:</p>
<pre><code class="javascript">var ollieTheCat = {};
ollieTheCat.furColor = 'gray';</code></pre>
<p>or by using bracket notation:</p>
<pre><code class="javascript">var ollieTheCat = new Object();
ollieTheCat['furColor'] = 'gray';</code></pre>
</section>
<section>
<h3>Accessing properties</h3>
<p>We can access properties with dot notation:</p>
<pre><code class="javascript">var furVariable = ollieTheCat.furColor;</code></pre>
<p>...or we can use bracket notation:</p>
<pre><code class="javascript">var furVariable = ollieTheCat['furColor'];</code></pre>
</section>
<section>
<h3>Changing properties</h3>
<p>Given an object:</p>
<pre><code class="javascript">var ollieTheCat = {
furColor: 'gray',
age: 1
};</code></pre>
<p>We can change the value of a property with dot notation:</p>
<pre><code class="javascript">ollieTheCat.furColor = 'hot pink';</code></pre>
<p>...or we can change the value with bracket notation:</p>
<pre><code class="javascript">ollieTheCat['furColor'] = 'hot pink';</code></pre>
</section>
<section>
<h3>Variables</h3>
<p>We can also use variables to store references to property names</p>
<pre><code class="javascript">var ollieTheCat = {
furColor: 'gray'
};
var keyVariable = 'furColor';
console.log(ollieTheCat[keyVariable]);</code></pre>
<p>Note that you can only use variables with bracket notation.</p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Create an animal object, and define these properties on it:</p>
<ul>
<li><code>name</code> - a string</li>
<li><code>description</code> - a string</li>
<li><code>noises</code> - an array of strings</li>
</ul>
</section>
<section>
<h3>Arrays of Objects</h3>
<pre><code class="javascript">var myCats = [
{ name: 'Ollie', age: 2, furColor: 'gray' },
{ name: 'Mabel', age: 7, furColor: 'gray' }
];</code></pre>
<p>What would be the result?</p>
<pre><code class="javascript">console.log(myCats[0].name);
console.log(myCats[1]furColor);
console.log(myCats[1]["Fur Color"]);</code></pre>
<br>
<a href="http://bit.ly/intermediate-js-array-of-objects" target=blank>http://bit.ly/intermediate-js-array-of-objects</a>
</section>
<section>
<h3>Iterating over properties of objects</h3>
<pre><code class="javascript">var zoo = {
birds: 3,
bears: 5,
cats: 12
};
for(var key in zoo) {
console.log(zoo[key] + ' ' + key);
}</code></pre>
<a href="http://bit.ly/intermediate-js-zoo" target=blank>http://bit.ly/intermediate-js-zoo</a>
</section>
<section>
<h3>Let's Develop It!</h3>
<p><a href="files/exercise-arrayofobjects.html" target="_new">Exercise instructions</a></p>
</section>
<section>
<h3>Nested Objects</h3>
<img src="img/nesting.png" height="359" width="705" alt="" style="max-height:300px;width:auto;">
<pre><code class="javascript">var doll = {
size: 'large',
innerDoll: { size: 'medium'}
};
doll.innerDoll.innerDoll = {size: 'small'};
console.log(doll);</code></pre>
</section>
<section>
<h2>Let's Develop It!</h2>
<p>Start with your array of animals from the previous exercise.</p>
<p>Add an <code>outfit</code> property to each animal, which contains an object that describes different parts of their outfit, for example, <code>shirt: "blue"</code>.</p>
<p>Log out the outfit of each animal to make sure it worked.</p>
<p><strong>Bonus:</strong> Modify your loop from the last exercise to include the animal's outfit in the output, e.g. "Daffy Duck is wearing blue pants and a white shirt."</p>
</section>
<section>
<h2>Questions?</h2>
<p>Questions on objects?</p>
</section>
<section>
<h1>Break time!</h1>
</section>
</div>
<footer>
<div class="copyright">
Intermediate JavaScript -- GDI Seattle --
<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>
</footer>
</div>
<script src="lib/js/head.min.js"></script>
<script src="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,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/accessibility-helper/js/accessibility-helper.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>