forked from anythingcodes/gdi-es6-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class2.html
1542 lines (1343 loc) · 78.3 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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Getting Started with ES6 and ES7 JavaScript</title>
<meta name="description" content="Materials and slides for a Girl Develop It ES6 JavaScript course. Includes ES7's minor additions.">
<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="dist/css/reveal.css">
<link rel="stylesheet" href="dist/css/gdidarkblue.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor --><link rel="stylesheet" href="dist/css/light.css">
<!-- dark editor <link rel="stylesheet" href="dist/css/dark.css">-->
<!-- <link rel="stylesheet" href="dist/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 = 'dist/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="dist/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="dist/js/html5shiv.js"></script>
<![endif]-->
</head>
<body spellcheck="false">
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening -->
<section class="small">
<h1>Getting Started with ES6 and ES7 JavaScript</h1>
<img src="dist/img/es6-gdi.jpg" alt="ES6 and ES7 Intro Image" class="noborder"/>
<h2 class="green">Class 2</h2>
</section>
<section>
<h2>Today's Topics</h2>
<ul>
<li>Review at-home challenge from last class</li>
<li>Arrow Functions</li>
<li>Arrow Functions and <code>this</code></li>
<li>Array Helpers</li>
<li>Altering Arrays</li>
</ul>
</section>
<section class="activity__challenge" data-background="#d2ccfc">
<h2>Review</h2>
<h3>Fruit Basket</h3>
<div class="small clear">
<div class="halfblock">
<pre><code contenteditable class ="javascript" >var fruit = 'apple';
(function basket(){
var fruit = 'banana';
console.log(fruit);
})();
console.log(fruit);
var fruitBasket = {
fruit: 'mango',
getFruit: function(){
console.log(fruit);
fruit = 'orange';
}
};
fruitBasket.getFruit();
console.log(fruit);</code></pre>
<a href="https://codepen.io/anythingcodes/pen/rmgNEb?editors=0012" target="_blank">View on CodePen »</a>
</div>
<div class="halfblock">
<p>What would the following output to the console and why?</p>
<div class="fragment" style="background-color:rgba(255,255,255,0.6);border: 1px solid black;padding:15px;margin: 15px 0">
<p><strong>Answer:</strong> <pre class="javascript"><code>banana
apple
apple
orange</code></pre>
</div>
<a class="fragment" target="_blank" href="http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/">Understand JavaScript's <code>this</code> with clarity, and master it »</a>
</div>
</div>
<aside class="notes">Go through the solution step by step by drawing a table on the board <a href="../../dist/img/fruit-basket-solution.png">that looks like this</a>. One column should be <code>Scopes</code> and the other should be <code>Output</code>. Within <code>Scopes</code>, you'll label the global `fruit` variable and the IIFE `fruit` variable and show how those variables change over time (global will change from apple to orange, while IIFE will only be banana). Walk students through the code, pointing to where different scopes' variables are assigned. More info on <code>this</code>: When a function is called as a method of an object, its <code>this</code> is set to the object the method is called on. Point out the article.
</aside>
</section>
<section data-background="#f05b62">
<h1 style="color: #fafafa;font-size:3.7em;">Arrow Functions</h1>
</section>
<section>
<h2>Arrow Functions</h2>
<div class="clear">
<div class="halfblock">
<h3 class="green">ES5</h3>
<pre><code contenteditable class ="javascript">var getMessage = function(name) {
return 'Hello ' + name + '!';
} </code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6</h3>
<pre><code contenteditable class ="javascript">const getMessage = name => `Hello ${name}!`;</code></pre>
</div>
</div>
<div class="clear small">
<div class="halfblock">
<h3 class="fragment">Making an arrow function:</h3>
<ol>
<li class="fragment">Remove the word <code>function</code></li>
<li class="fragment">Place a fat arrow (<code>=></code>) after parameters</li>
</ol>
</div>
<div class="halfblock">
<h3 class="blue fragment">Trimming it down:</h3>
<ul>
<li class="fragment">If there's only <span class="green">one parameter</span>, remove the surrounding parentheses <code>()</code></li>
<li class="fragment">If there's only <span class="green">one expression</span> in the function body, remove <code>{ }</code> and <code>return</code> (<span class="orange">implicit return</span>)</li>
</ul>
</div>
</div>
<aside class="notes">
<ul>
<li>Refactor ES5 version in front of students. Ask about <code>const</code> and template strings</li>
<li>Skinny arrow is <code>-></code> like in Ruby and Coffeescript</li>
<li>If there are zero or more than one args, you need to include parentheses, e.g. <code>const sum = () => retVal;</code></li>
</ul>
</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activity</h2>
<h3>Writing arrow functions</h3>
<p>Refactor the following code to use arrow functions.</p>
<div class="clear" style="min-height:300px;">
<div class="halfblock">
<h6 class="light">Activity 1</h6>
<p data-height="200" data-theme-id="0" data-slug-hash="dWwdLM" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Arrow Function Exercise #1" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/dWwdLM/">Arrow Function Exercise #1</a> by Liz Shaw (<a href="https://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/NjeymY?editors=0010" target="_blank">View Solution</a>
</div>
</div>
<div class="halfblock">
<h6 class="light">Activity 2</h6>
<p data-height="200" data-theme-id="0" data-slug-hash="mmBZYo" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Arrow functions" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/mmBZYo/">Arrow functions</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/JNwpqd?editors=0010#0" target="_blank">View Solution</a>
</div>
</div>
</div>
<aside class="notes">
Activity 2 is a common interview question
</aside>
</section>
<section data-background="#f05b62">
<h1 style="color: #fafafa;font-size:3em;">Arrow Functions and <code>this</code></h1>
</section>
<section>
<h2>The <code>this</code> keyword</h2>
<p>The <code>this</code> keyword determines context</p>
<pre class="small"><code class="javascript" contenteditable>const obj = {
name: 'Zeta',
getName: function() {
console.log(this.name);
}
};
obj.getName(); // Zeta</code></pre>
<aside class="notes">
We saw the <code>this</code> keyword earlier with Fruit Baskets. Seems straightforward when you have a smaller codebase and more simple objects, but when you introduce multiple scopes and functions it quickly spirals out of control.
</aside>
</section>
<section>
<h2>There's <code>this</code> One Problem</h2>
<p class="fragment">Binding and changing <code>this</code> in functions is one of the most common JavaScript errors</p>
<div class="fragment clear" style="margin-top:.5em;">
<p><img src="dist/img/computer-frustration.gif" alt="Computer frustration" class="right" />It's possible to mistakenly affect one object when you meant to affect another</p>
<p class="fragment"><a href="https://twitter.com/bendhalpern/status/578925947245633536" target="_blank"><img src="dist/img/this-tweet-joke.png" alt="JavaScript `this` tweet by Ben Halpern" /></a></p>
</div>
<!--<p class="fragment"><a href="https://codepen.io/anythingcodes/pen/BRvYQp/?editors=0010" target="_blank">Let's take a look »</a></p>-->
<aside class="notes">Ask students what the output to console be when obj.getName() is called? This is straightforward with a small codebase but it quickly spirals out of control with nested functions.</aside>
</section>
<section>
<h2>What is <code>this</code> problem?</h2>
<h3>Let's take a look</h3>
<div style="min-height:200px;" class="fragment">
<p data-height="200" data-theme-id="0" data-slug-hash="qmPGJN" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Arrow functions and `this`" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/qmPGJN/">Arrow functions and `this`</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<a class="solution solution__lesson-2" href="https://codepen.io/anythingcodes/pen/QvqReo?editors=0010#0" target="_blank">View Solution</a>
</div>
<aside class="notes">
<ul>
<li>Ask students: What would you expect to happen?</li>
<li>Ask students: What are some ways around the problem that you've seen?</li>
<li>Refer to it as 'the this keyword' and then 'the keyword' going forward</li>
<li>The call to this.logEvent() is broken because <code>this</code> is a reference to the object that was the target of the event (<code>document</code>)</li>
<li>Ask students to transform existing code to arrow function per earlier steps
<ul>
<li>Can fix by binding <code>this</code> to logEvent(), but this creates a new function whose <code>this</code> is bound to the current <code>this</code>.</li>
<li>Could also declare <code>var self = this;</code></li>
</ul>
</li>
</ul>
</aside>
</section>
<section>
<h2>Can arrow functions help?</h2>
<ul>
<li class="fragment">Arrow functions <span class="green">do not have a <code>this</code> 🌟</li>
<li class="fragment">The value of <code>this</code> is <span class="orange">lexically fetched</span> from the scope the arrow function sits inside</li>
</ul>
<div style="min-height:200px;" class="fragment">
<p data-height="200" data-theme-id="0" data-slug-hash="qmPGJN" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Arrow functions and `this`" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/qmPGJN/">Arrow functions and `this`</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<a class="solution solution__lesson-2" href="https://codepen.io/anythingcodes/pen/QvqReo?editors=0010#0" target="_blank">View Solution</a>
</div>
<aside class="notes">
<ul>
<li>Arrow functions are a better way to fix the issue! ES6's purpose is to solve this problem and stop worrying about context.</li>
</ul>
</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activity</h2>
<h3>Fixing <code>this</code> scoping with arrow functions</h3>
<div style="min-height: 150px;">
<p data-height="150" data-theme-id="0" data-slug-hash="qmPzPP" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="Arrow Functions Activity" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/qmPzPP/">Arrow Functions Activity</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
</div>
<a href="https://codepen.io/anythingcodes/pen/eWGwbv?editors=0010" class="solution__lesson-1 solution" target="_blank">View Solution</a>
</section>
<section data-background="#f05b62">
<h1 style="color: #fafafa;font-size:3.7em;">Array Helpers</h1>
</section>
<section>
<h2>Array Helpers</h2>
<p>Tons of new methods that can be applied to arrays</p>
<table class="data compact fragment">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.forEach((element, index, array) => { });</code></td>
<td>loop through elements in an array</td>
<td><code>undefined</code></td>
</tr>
<tr>
<td><code class="javascript">arr.every((element, index, array) => { });</code></td>
<td>check if <span class="blue">every</span> element passes a test</td>
<td><code>true||false</code></td>
</tr>
<tr>
<td><code class="javascript">arr.some((element, index, array) => { });</code></td>
<td>check if <span class="blue">some</span> (at least 1) <code>arr</code> elements pass a test</td>
<td><code>true||false</code></td>
</tr>
<tr>
<td><code class="javascript">arr.filter((element, index, array) => { });</code></td>
<td>create a new array by <span class="blue">filtering</span> the original array elements</td>
<td><code>Array</code></td>
</tr>
<tr>
<td><code class="javascript">arr.map((element, index, array) => { });</code></td>
<td>create a new array by <span class="blue">modifying</span> the original array elements</td>
<td><code>Array</code></td>
</tr>
<tr>
<td><code class="javascript">arr.find((element, index, array) => { });</code></td>
<td>find the <span class="blue">first element that passes a test</span></td>
<td>1 element</td>
</tr>
<tr>
<td><code class="javascript">arr.findIndex((element, index, array) => { });</code></td>
<td>find the <span class="blue">index of the first element that passes a test</span></td>
<td>1 element</td>
</tr>
<tr>
<td><code class="javascript">arr.reduce((calculatedValue, element, index, array) => { }, initialValue);</code></td>
<td>pass in an <code>initialValue</code> and modify it according to the current <code>element</code> value</td>
<td>1 reduced value</td>
</tr>
</table>
<aside class="notes">
Convenient methods applied to arrays. We'll go over each of these individually, and run through exercises. Point out that reduce() is the odd one out. It takes different parameters. Note that you don't need to use ALL the parameters, just the ones you need.
</aside>
</section>
<section>
<h2>Array Helpers</h2>
<h3>Moving away from for loops</h3>
<ul>
<li class="fragment">Easier for other devs to immediately read and understand</li>
<li class="fragment">Encourage functional programming</li>
<li class="fragment">More compact — why write more code?</li>
<li class="fragment">More maintainable and scalable</li>
</ul>
<aside class="notes">Nothing wrong with for loops, but you probably don't need them anymore. Array helpers describe your intent more easily to your teammates, so it's a best practice.</aside>
</section>
<section>
<h2>forEach</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.forEach((element, index, array) => { });</code></td>
<td>loop through elements in an array</td>
<td><code>undefined</code></td>
</tr>
</table>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var names = ['Morgan', 'Taylor', 'Lesley'];
for (var i = 0; i < names.length; i++) {
console.log(names[i]);
}
</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: forEach</h3>
<pre><code contenteditable class ="javascript">const names = ['Morgan', 'Taylor', 'Lesley'];
names.forEach(function(name, index, array) {
console.log(name);
});</code></pre>
</div>
<p><a href="http://codepen.io/anythingcodes/pen/KagmxL?editors=0012" target="_blank">View CodePen »</a></p>
</div>
<aside class="notes">
Activity on this slide: Ask class to arrow functionitize the right-hand ES6 block. Transform it in front of students!
</aside>
</section>
<section>
<h2>forEach</h2>
<code>arr.forEach(iteratorFunction)</code>
<pre class="small javascript"><code contenteditable>const numbers = [123, 500, -50, 82, 8];
numbers.forEach(function(number, index, array) {
console.log(number > 50); // can you get this on one line?
});</code></pre>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>The iterator function runs once for each element in the array</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-forEach.svg" alt="forEach diagram" style="max-height: 47vh;"/></p>
</div>
</div>
<aside class="notes">
Ask students what they'd expect to see in the console. Code the arrow function version in front of students. Mention that all array helpers in the class will use arrow functions going forward. Clarify how the iterator function automatically receives the current element, in this case <code>number</code>. Point out the convention of using the plural form as the array name (numbers) and the singular form (number) in your arrow function.
</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activities</h2>
<h3>Using the forEach Helper</h3>
<p class="small">Modify the code between <br /><code>// [START] your code here</code> and <code>// [END] your code here</code></p>
<div class="clear" style="min-height:300px;">
<div class="halfblock">
<h6 class="light">Activity 1</h6>
<p data-height="200" data-theme-id="0" data-slug-hash="rjMNEJ" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="ES6 Lesson 1: forEach #2" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/rjMNEJ/">ES6 Lesson 1: forEach #2</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/XpjJJq?editors=0010#0" target="_blank">View Solution</a>
</div>
</div>
<div class="halfblock">
<h6 class="light">Activity 2</h6>
<p data-height="200" data-theme-id="0010" data-slug-hash="mRrdEQ" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="ES6 Lesson 1: forEach #1" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/mRrdEQ/">ES6 Lesson 1: forEach #1</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/jyMOeM?editors=0010" target="_blank">View Solution</a>
</div>
</div>
</div>
</section>
<section>
<h2>every</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.every((element, index, array) => { });</code></td>
<td>check if <span class="blue">every</span> element passes a test</td>
<td><code>true||false</code></td>
</tr>
</table>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var students = [
{ name: 'Morgan', present: true },
{ name: 'Sam', present: false },
{ name: 'Taylor', present: true }
];
var allPresent = true;
for (var i = 0; i < students.length; i++) {
if (!students[i].present) {
allPresent = false;
break;
}
}
console.log(allPresent); // false
</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: every</h3>
<pre><code contenteditable class ="javascript">const students = [
{ name: 'Morgan', present: true },
{ name: 'Sam', present: false },
{ name: 'Taylor', present: true }
];
const allPresent = students.every(student => student.present);
console.log(allPresent); // false</code></pre>
<p><a href="https://codepen.io/anythingcodes/pen/bWYKGW?editors=0012" target="_blank">View CodePen »</a></p>
</div>
</div>
<aside class="notes">
Ask students: Which is easier to understand? Written in ES6, it’s so much cleaner! Only a few lines of code. Engineer will see ‘every’ and be able to instantaneously able to tell what the function does since they’ll recognize ‘every’ and have a reasonable idea of what it does.
</aside>
</section>
<section>
<h2>every</h2>
<code>arr.every(iteratorFunction)</code>
<pre class="small javascript"><code contenteditable>const numbers = [123, 500, -50, 82, 8];
const allLargerThan50 = numbers.every(number => number > 50);
console.log(allLargerThan50); // false</code></pre>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:80%;float: left">
<p>Runs until the iterator function returns <code>false</code>, in order to check that <span class="green">every element</span> passes a test</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-every.svg" alt="every diagram" style="max-height: 47vh;"/></p>
</div>
</div>
<aside class="notes">Runs until iteratorFunction returns false</aside>
</section>
<section>
<h2>some</h2>
<table class="data compact fragment">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.some((element, index, array) => { });</code></td>
<td>check if <span class="blue">some</span> (at least 1) <code>arr</code> elements pass a test</td>
<td><code>true||false</code></td>
</tr>
</table>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var students = [
{ name: 'Morgan', present: true },
{ name: 'Sam', present: false },
{ name: 'Taylor', present: true }
];
var somePresent = false;
for (var i = 0; i < students.length; i++) {
if (students[i].present) {
somePresent = true;
break;
}
}
console.log(somePresent); // true
</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: some</h3>
<pre><code contenteditable class ="javascript">const students = [
{ name: 'Morgan', present: true },
{ name: 'Sam', present: false },
{ name: 'Taylor', present: true }
];
const somePresent = students.some(student => student.present);
console.log(somePresent); // true</code></pre>
<p><a href="https://codepen.io/anythingcodes/pen/gWXKbM?editors=0012" target="_blank">View CodePen »</a></p>
</div>
</div>
<aside class="notes">
<ul>
<li>Before revealing slide contents, ask students: Now that we know about every(), what do you think some() would do?</li>
<li>some() = <em>at least one</em></li>
</ul>
</aside>
</section>
<section>
<h2>some</h2>
<code>arr.some(iteratorFunction)</code>
<pre class="small javascript"><code contenteditable>const numbers = [123, 500, -50, 82, 8];
const someLargerThan50 = numbers.some(number => number > 50);
console.log(someLargerThan50); // true</code></pre>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>Runs until the iterator function returns <code>true</code> (i.e. when <span class="green">some elements</span> pass a test)</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-some.svg" alt="some diagram" style="max-height: 47vh;"/></p>
</div>
</div>
<p> </p>
<aside class="notes">At least 1</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activity</h2>
<h3>Using the <code>every</code> and <code>some</code> helpers</h3>
<div class="clear">
<div class="halfblock">
<h3>Activity 1</h3>
<div style="min-height: 260px;">
<p data-height="260" data-theme-id="0" data-slug-hash="mmqKEE" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="every() and some() activity" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/mmqKEE/">every() and some() activity</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
</div>
<a href="https://codepen.io/anythingcodes/pen/BZBgdE?editors=0010" target="_blank" class="solution solution__lesson-2">View Solution</a>
</div>
<div class="halfblock">
<h3>Activity 2</h3>
<div style="min-height: 260px;">
<p data-height="260" data-theme-id="0" data-slug-hash="WjXyXz" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="every() and some() activity" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/WjXyXz/">every() and some() activity #2</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
</div>
<a href="https://codepen.io/anythingcodes/pen/xrKoPL?editors=0010" target="_blank" class="solution solution__lesson-2">View Solution</a>
</div>
</div>
</section>
<section>
<h2>filter</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.filter((element, index, array) => { });</code></td>
<td>create a new array by <span class="blue">filtering</span> the original array elements</td>
<td><code>Array</code></td>
</tr>
</table>
<p class="fragment"><code>const filteredArr = arr.filter(iteratorFunction);</code></p>
<div class="clear fragment small">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var users = [
{ username: 'ryan10', active: true },
{ username: 'morgan', active: false }
];
var activeUsers = [];
for (var i = 0; i < users.length; i++) {
if (users[i].active) {
activeUsers.push(users[i]);
}
}
console.log(activeUsers);</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6</h3>
<pre><code contenteditable class ="javascript">const users = [
{ username: 'ryan10', active: true },
{ username: 'morgan', active: false }
];
const activeUsers = users.filter(user => user.active);
console.log(activeUsers);</code></pre>
<p class="small"><a href="http://codepen.io/anythingcodes/pen/LxRqOZ?editors=0012" target="_blank">CodePen example with non-boolean »</a></p>
</div>
</div>
<aside class="notes">
Don't need to use an if statement, since <code>user.active</code> returns a boolean. Also <code>home.price < 600000</code> returns a boolean. With <code>filter()</code>, you are not modifying the array; you're just returning an array of items that match a certain test.
</aside>
</section>
<section>
<h2>filter</h2>
<code>arr.filter(iteratorFunction)</code>
<pre class="small javascript fragment"><code contenteditable>const numbers = [123, 500, -50, 82, 8];
const numbersLargerThan50 = numbers.filter(number => number > 50);
console.log(numbersLargerThan50); // [ 123, 500, 82 ]</code></pre>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>Only the <span class="green">elements that return true</span> are included in the resulting array</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-filter.svg" alt="filter diagram" style="max-height: 47vh;"/></p>
</div>
</div>
<p> </p>
<aside class="notes">With filter(), the resulting array is always less than or equal to the size of the original array</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activities</h2>
<h3>Using the filter helper</h3>
<div class="clear">
<div class="halfblock">
<h6 class="light">Activity 1</h6>
<p data-height="250" data-theme-id="0" data-slug-hash="GrNKXy" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="ES6 Lesson 1: filter #2" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/GrNKXy/">ES6 Lesson 1: filter #2</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<p class="clear blue small" style="background-color: #fff;border: 1px solid black;padding: 5px 0;margin:15px 0"><strong>Activity 1 Tip:</strong> A number is even if<br /><code style="background-color:white;color:black;">number % 2 === 0</code></p>
<p>
<a class="solution solution__lesson-2" href="https://codepen.io/anythingcodes/pen/eWwaWN?editors=0010" target="_blank">View Solution</a>
</p>
</div>
<div class="halfblock">
<h6 class="light">Activity 2</h6>
<p data-height="250" data-theme-id="0" data-slug-hash="NdRoML" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="ES6 Lesson 1: filter #1" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/NdRoML/">ES6 Lesson 1: filter #1</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<p>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/ggwERZ?editors=0012" target="_blank">View Solution</a>
</p>
</div>
</div>
<p> </p>
</section>
<section>
<h2><code>filter()</code> Practical Example</h2>
<h3>Filtering API Responses</h3>
<div class="fragment-progression">
<div class="fragment"><img src="dist/img/comments-post-data__1.svg" alt="The data relationship between comments and posts - step 1" /></div>
<div class="fragment"><img src="dist/img/comments-post-data__2.svg" alt="The data relationship between comments and posts - step 2" /></div>
<div class="fragment"><img src="dist/img/comments-post-data__3.svg" alt="The data relationship between comments and posts - step 3" /></div>
<div class="fragment"><img src="dist/img/comments-post-data__4.svg" alt="The data relationship between comments and posts - step 4" /></div>
<div class="fragment"><img src="dist/img/comments-post-data__5.svg" alt="The data relationship between comments and posts - step 5" /></div>
<div class="fragment"><img src="dist/img/comments-post-data.svg" alt="The data relationship between comments and posts" /></div>
</div>
<p class="small">
<a href="https://jsonplaceholder.typicode.com/comments" target="_blank">API Response Example »</a><br /><a href="http://codepen.io/anythingcodes/pen/ggLYoX?editors=0012" target="_blank">CodePen Example »</a>
</p>
<aside class="notes">
Describe one common use case for the filter helper, including filtering JSON to only reveal the comments associated with a specific post (e.g. postId 23 for post with ID for 23). Mention that we'll code an example of this on the next slide. Mention students should focus on refactoring lines 21 through 28.
</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activities</h2>
<h3>Filtering API responses</h3>
<h6 class="light">Activity 3</h6>
<p data-height="250" data-theme-id="0" data-slug-hash="PWGLeg" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="ES6 Lesson 1: filter #2" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/PWGLeg/">ES6 Lesson 1: filter #3</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<p>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/LxRqaW?editors=1010" target="_blank">View Solution</a>
</p>
<p class="clear blue small" style="background-color: #fff;border: 1px solid black;padding: 5px 0;margin:15px 0"><strong>Tip:</strong> Try to use more than one array helper. Are there any other ES6 tricks you could use to clean up this code?</p>
<p> </p>
</section>
<section class="activity__challenge" data-background="#d2ccfc">
<h2>At-Home Challenge: Implement a <code>reject</code> function</h2>
<div class="small clear">
<div class="halfblock">
<pre><code contenteditable class ="javascript" >function reject(array, iteratorFunction) {
// your code here
}</code></pre>
<div class="fragment small" style="background-color:rgba(255,255,255,0.6);border: 1px solid black;padding:15px;margin: 15px 0">
<p><strong>Hint:</strong> You may use <em>filter</em>.
</div>
<p>Fork <a href="https://codepen.io/anon/pen/dRbWJv?editors=0010" target="_blank">this CodePen</a> for some starter code.</p>
</div>
<div class="halfblock">
<p class="small">Reject should work opposite to filter; if a function returns <code>true</code>, the item should <em>not</em> be included in the new array, for example:<br /> </p>
<pre><code contenteditable class ="javascript" >const numbers = [ 5, 10, 15, 17, 18, 20 ];
const lessThanEighteen = reject(numbers, number => number >= 18);
console.log(lessThanEighteen);
// returns [ 5, 10, 15, 17 ];</code></pre>
</div>
</div>
<!--<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/VPmZNz?editors=0012" target="_blank">View Solution</a>
</div>-->
</section>
<section>
<h2>map</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.map((element, index, array) => { });</code></td>
<td>create a new array by <span class="blue">modifying</span> the original array elements</td>
<td><code>Array</code></td>
</tr>
</table>
<p> </p>
<p class="fragment">You can <span class="blue">assign the resulting array</span> to a variable:<br /><code>const modifiedArr = arr.map(iteratorFunction);</code></p>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var numbers = [2, 6, 10];
var halvedNumbers = [];
for (var i = 0; i < numbers.length; i++) {
halvedNumbers.push(numbers[i] / 2);
}
console.log(halvedNumbers); // 1, 3, 5
</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: map</h3>
<pre><code contenteditable class ="javascript" >const numbers = [2, 6, 10];
const halvedNumbers = numbers.map(number => number / 2);
console.log(halvedNumbers); // 1, 3, 5
</code></pre>
</div>
<p><a href="http://codepen.io/anythingcodes/pen/MJjoxm?editors=0012" target="_blank">View CodePen »</a></p>
</div>
<p class="small orange fragment">No need to create an empty array first! 🎉</p>
<aside class="notes">
Primary difference between forEach and map: <em>forEach</em> iterates through an array but <span class="orange">doesn't return anything</span>
</aside>
</section>
<section>
<h2>map</h2>
<code>arr.map(iteratorFunction)</code>
<pre class="small javascript"><code contenteditable>const numbers = [123, 500, -50, 82, 8];
const decrementedNumbers = numbers.map(number => number - 1);
console.log(decrementedNumbers); // [ 122, 499, -51, 81, 7 ]</code></pre>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>The iterator function runs once for each element in the array</p>
<p class="fragment info blue">Be sure to return a value each iteration!</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-map.svg" alt="map diagram" style="max-height: 47vh;"/></p>
</div>
</div>
<aside class="notes">
Difference between forEach() and map(): map() returns an array at the end. Don't use map if you just need to loop through an array. forEach doesn't return anything, but map <em>modifies</em> the previous array values ti create a new array.
</aside>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activities</h2>
<h3>Using the map Helper</h3>
<div class="clear" style="min-height:350px;">
<div class="halfblock">
<h6 class="light">Activity 1</h6>
<p data-height="350" data-theme-id="0" data-slug-hash="JERyKw" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="ES6 Lesson 1: map #2" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/JERyKw/">ES6 Lesson 1: map #2</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/QdKMaq?editors=0012" target="_blank">View Solution</a>
</div>
</div>
<div class="halfblock">
<h6 class="light">Activity 2</h6>
<p data-height="350" data-theme-id="0" data-slug-hash="RKGZQW" data-default-tab="js" data-user="anythingcodes" data-embed-version="2" data-pen-title="ES6 Lesson 1: map #1" class="codepen">See the Pen <a href="http://codepen.io/anythingcodes/pen/JERyKw/">ES6 Lesson 1: map #1</a> by Liz Shaw (<a href="http://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="http://codepen.io">CodePen</a>.</p>
<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/xgELgm?editors=0012" target="_blank">View Solution</a>
</div>
</div>
</div>
</section>
<section class="activity__challenge" data-background="#d2ccfc">
<h2>At-Home Challenge: Implement a <code>pluck</code> function</h2>
<div class="small clear">
<div class="halfblock">
<pre><code contenteditable class ="javascript" >function pluck(array, property) {
// your code here
}</code></pre>
<div class="fragment small" style="background-color:rgba(255,255,255,0.6);border: 1px solid black;padding:15px;margin: 15px 0">
<p><strong>Hint:</strong> Access a property on an object by using square bracket notation. For example:<br /> </p>
<pre><code contenteditable class ="javascript" >const animal = { type: 'Dinosaur', status: 'Extinct' };
animal['type']; // returns 'Dinosaur'</code></pre>
</div>
</div>
<div class="halfblock">
<p class="small">Pluck should accept an array and a string representing a property name and return an array containing that property from each object, for example:<br /> </p>
<pre><code contenteditable class ="javascript" >const cats = [ { name: 'Colonel Meow' }, { name: 'Lil Bub' }, { name: 'Grumpy Cat' }];
pluck(cats, 'name');
// returns ['Colonel Meow', 'Lil Bub', 'Grumpy Cat'];
</code></pre>
</div>
</div>
<!--<div>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/pRErMO?editors=0012" target="_blank">View Solution</a>
</div>-->
</section>
<section>
<h2>find</h2>
<table class="data compact">
<tr>
<th>Array Helper</th>
<th>Use it to...</th>
<th>Returns</th>
</tr>
<tr>
<td><code class="javascript">arr.find((element, index, array) => { });</code></td>
<td>find the <span class="blue">first element that passes a test</span></td>
<td>1 element</td>
</tr>
</table>
<p class="fragment"><code>const matchingObject = arr.find(iteratorFunction);</code></p>
<div class="small clear fragment">
<div class="halfblock">
<h3 class="green">ES5: for loop</h3>
<pre><code contenteditable class ="javascript">var jobs = [
{ title: 'Electrician' },
{ title: 'Developer' },
{ title: 'Barista' }
];
var devJob;
for (var i = 0; i < jobs.length; i++) {
if (jobs[i].title === 'Developer') {
devJob = jobs[i];
break;
}
}
console.log(devJob); // { title: 'Developer' }</code></pre>
</div>
<div class="halfblock fragment">
<h3 class="green">ES6: find</h3>
<pre><code contenteditable class ="javascript">const jobs = [
{ title: 'Electrician' },
{ title: 'Developer' },
{ title: 'Barista' }
];
const devJob = jobs.find(job => job.title === 'Developer');
console.log(devJob); // { title: 'Developer' }</code></pre>
<p class="small"><a href="http://codepen.io/anythingcodes/pen/ggLbMO?editors=0012" target="_blank">CodePen Example »</a></p>
</div>
</div>
<aside class="notes">
Don't need to use an if statement, since <code>user.active</code> returns a boolean. Also <code>home.price < 600000</code> returns a boolean.
</aside>
</section>
<section>
<h2>find</h2>
<p><code>arr.find(iteratorFunction)</code></p>
<div class="small"><pre class="small"><code contenteditable class ="javascript">const jobs = [
{ title: 'Electrician' },
{ title: 'Developer' },
{ title: 'Barista' }
];
const devJob = jobs.find(job => job.title === 'Developer');
console.log(devJob); // { title: 'Developer' }</code></pre></div>
<div class="clear fragment" style="margin-top:.5em;">
<div style="width:25%; font-size:75%;float: left">
<p>Runs until the iterator function returns <code>true</code>. When it does, the element at that index is returned.</p>
</div>
<div style="width:75%; float: left">
<p><img src="dist/img/diagram-find.svg" alt="find diagram" style="max-height: 40vh;"/></p>
</div>
</div>
</section>
<section>
<h2><code>find()</code> Practical Example</h2>
<h3>Single-page web apps</h3>
<p><img src="dist/img/single-page-app__find.png" alt="The find helper in a single-page web app" /></p>
</section>
<section class="activity" data-background="rgb(6, 116, 123)">
<h2>Activities</h2>
<h3>Using the find helper</h3>
<div style="min-height:250px;">
<p data-height="250" data-theme-id="0" data-slug-hash="aWrgYQ" data-default-tab="js,result" data-user="anythingcodes" data-embed-version="2" data-pen-title="the find() array helper" class="codepen">See the Pen <a href="https://codepen.io/anythingcodes/pen/aWrgYQ/">the find() array helper</a> by Liz Shaw (<a href="https://codepen.io/anythingcodes">@anythingcodes</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<p>
<a class="solution solution__lesson-2" href="http://codepen.io/anythingcodes/pen/ZKNdMR?editors=0012" target="_blank">View Solution</a>
</p>
</div>
</section>
<section class="activity__challenge" data-background="#d2ccfc">
<h2>At-Home Challenge: Write a <code>findWhere</code> function</h2>
<div class="clear small" style="margin-top:0">
<div class="halfblock">
<p>Often we need to find an object with a given property and value. Write a <code>findWhere</code> function that accepts two parameters, the array and the criteria as an object, and returns the matching object. You may assume that the criteria is one key-value pair. Fork <a href="https://codepen.io/anythingcodes/pen/VbOoOz?editors=0012" style="font-weight: bold;" target="_blank">this CodePen</a> for some starter code.</p>
<div class="fragment" style="font-size:70%;background-color:rgba(255,255,255,0.6);border: 1px solid black;padding:15px;margin: 15px 0">
<p><strong>Hint:</strong> Use <code style="background-color:white;color:black;">Object.keys(criteria)[0]</code> to figure out the name of the property on the object. For example, <code style="background-color:white;color:black;">Object.keys({ type: 'marsupial' })</code> would return <code style="background-color:white;color:black;">'type'</code>. You could then check to see if a given element in the array had a property equal to the criteria's value with something like <code style="background-color:white;color:black;">element[property] === criteria[property]</code>.</p>
</div>
</div>
<div class="halfblock">
<p>The following code ...</p>
<pre><code contenteditable class="javascript">const animals = [
{ id: 0, name: 'iguana', type: 'reptile' },
{ id: 1, name: 'cardinal', type: 'bird' },
{ id: 2, name: 'platypus', type: 'marsupial' },
{ id: 3, name: 'frog', type: 'amphibian' }
];