-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass1.html
676 lines (607 loc) · 22.4 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 1 ~ Intermediate JS ~ Girl Develop It</title>
<meta name="description"
content="This is a proposed Girl Develop It NYC Intermediate JavaScript curriculum. The course is meant to be taught in 4 two-hour sections.">
<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/gdidefault.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">
<div class="slides">
<section>
<img src="img/circle-gdi-logo.png" alt="GDI Logo" class="noborder">
<h2>Intermediate JavaScript</h2>
<h3>Class 1</h3>
</section>
<section>
<h3>Get the course!</h3>
<p>Download the course files - <a
href="https://github.com/gdi-nyc/gdi-intermediate-javascript">https://github.com/gdi-nyc/gdi-intermediate-javascript</a>
</p>
<img src="img/github_download.PNG" alt="download course instruction">
<p>Can view slides at - <a
href="https://gdi-nyc.github.io/gdi-intermediate-javascript/">https://gdi-nyc.github.io/gdi-intermediate-javascript/</a>
</p>
</section>
<section>
<h1>Agenda</h1>
<ul>
<li>Intro</li>
<li>JavaScript Programming Refresher</li>
<li>Some JS conveniences</li>
</ul>
</section>
<!-- Welcome-->
<section class="hide-pdf">
<h3>Welcome!</h3>
<div class="left-align">
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship
and hands-on instruction.</p>
<ul>
<li>Instructor & TA Intro</li>
<li>Classroom == Safe Space</li>
<li>Help each other</li>
<li>Have fun!</li>
</ul>
</div>
</section>
<section>
<h1 class="green">Some "rules"</h1>
<ul>
<li class="fragment"><a href="https://www.girldevelopit.com/code-of-conduct" target="_blank">GDI Code of
Conduct</a></li>
<li class="fragment">Every question is important</li>
<li class="fragment">Failure = learning</li>
<li class="fragment">Your keyboard is lava (for instructors)</li>
</ul>
</section>
<section>
<h3>Quick Logistics!</h3>
<div class="left-align">
<ul>
<li>Where's the bathroom?</li>
<li>Who's camera shy?</li>
<li>Get on Slack!</li>
</ul>
</div>
</section>
<section>
<h3>Breaking the ice!</h3>
<ul>
<li>What's your name?</li>
<li>What do you hope to get out of this class?</li>
</ul>
</section>
<section>
<p><a href="mainProject/class_4/index_SOLUTION.html" target="_blank">This</a> is what we'll be making!</p>
<img src="img/quiz_app.PNG" alt="">
</section>
<section>
<h1>JavaScript Refresher!</h1>
</section>
<section>
<h2>What is JavaScript?</h2>
<div class="fragment">
<img src="img/js.png" alt="" width="300px">
<p>An object-oriented computer programming language commonly used to create interactive effects
within web browsers.</p>
</div>
</section>
<section>
<h2>JS History</h2>
<ul>
<li class="fragment" style="position: relative;">
<p>Created by Brendan Eich in 1995</p>
<img src="img/brendan.jpg" alt="" width="150px" height="150px" style="position: absolute; left: -250px; top:-150px; max-height: none;">
</li>
<li class="fragment">Inspired by Java</li>
<li class="fragment">Standardized - European Computer Manufacturers Association Script (ECMAScript)</li>
<li class="fragment">ECMAScript 1 (1997)</li>
<li class="fragment">ECMAScript 2 (1998)</li>
<li class="fragment">ECMAScript 3 (1999)</li>
<li class="fragment">ECMAScript 5 (2009)</li>
<li class="fragment">ECMAScript 2015 (AKA ES6)</li>
</ul>
</section>
<section>
<h2>Variables</h2>
<p>Variables store information</p>
<pre class="fragment"><code class="javascript" data-trim>
let age = 26; // Can change this variable
const name = "Marry"; // Cannot change this variable
</code></pre>
<p class="fragment">
Note: best practice is to prefer using "const"
</p>
</section>
<section>
<h2>Data Types</h2>
<p>Three most common types of data</p>
<pre class="fragment"><code class="javascript" data-trim>
56; // Number
"blah blah"; // String
true; // Boolean
</code></pre>
</section>
<section>
<h2>POP QUIZ!</h2>
<p>Name that data type!</p>
<p class="fragment">17</p>
<p class="fragment">false</p>
<p class="fragment">"false"</p>
<p class="fragment">1</p>
<p class="fragment">"525600"</p>
<p class="fragment">true</p>
</section>
<section>
<h2>POP QUIZ - Answers!</h2>
<p>Name that data type!</p>
<p>17 - Number</p>
<p>false - Boolean</p>
<p>"false" - String</p>
<p>1 - Number</p>
<p>"525600" - String</p>
<p>true - Boolean</p>
</section>
<section>
<h2>Math Operators</h2>
<pre><code class="javascript" data-trim>
5 + 6; // 11
9 - 4; // 5
3 * 5; // 15
4 / 2; // 2
2 ** 3; // 8
16 % 5; // 1
</code></pre>
<p class="fragment">The last one, %, is called modulo - it returns the remainder of the division</p>
<p class="fragment">Modulo is often used to check for even division</p>
<pre class="fragment"><code class="javascript" data-trim>
// Checking for evenness
let amount = 2206;
amount % 2; // 0
amount % 2 === 0; // true
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
361 % 8; // 1
361 % 8 === 0; // false
</code></pre>
</section>
<section>
<h2>Arrays</h2>
<ul>
<li class="fragment">Arrays store multiple pieces of information, or items</li>
<li class="fragment">Each item is referenced by it's index, or spot in "line"</li>
<li class="fragment">Order matters!</li>
<li class="fragment">Indexing starts at 0!</li>
</ul>
<pre class="fragment"><code class="javascript" data-trim>
const students = ["Cheryl", "Sammy","Marty"];
students[0]; // "Cheryl"
students[1]; // "Sammy"
students[0] = "Billy"; // reassignment
console.log(students); // ["Billy", "Sammy","Marty"]
students[5]; // undefined
</code></pre>
</section>
<section>
<h2>POP QUIZ!</h2>
<pre class="fragment"><code class="javascript" data-trim>
const colors = ["blue","red","green","cyan","yellow","orange","#000000"];
</code></pre>
<p class="fragment">How would you access "cyan"?</p>
<pre class="fragment"><code class="javascript" data-trim>
colors[3];
</code></pre>
<p class="fragment">What string is at the 5th index?</p>
<pre class="fragment"><code class="javascript" data-trim>
colors[5]; // "orange"
</code></pre>
<p class="fragment">How would you access the last item in the array?</p>
<pre class="fragment">
<code class="javascript" data-trim>
colors[6]; // technically correct...
colors[colors.length-1]; //👍 programmers #1 choice
</code></pre>
<p class="fragment">How would you reassign the last item to "black"?</p>
<pre class="fragment"><code class="javascript" data-trim>
colors[colors.length-1] = "black";
</code></pre>
</section>
<section>
<h2>Loops</h2>
<p>Loops allows the same block of code to be executed multiple times in succession</p>
<p class="fragment">The 2 most common loops are the "for" and "while" loop</p>
</section>
<section>
<h2>"for" loop</h2>
<img style="background-color: white;" width="500px" src="img/for_loop.png" alt="for loop breakdown"
class="fragment">
</section>
<section>
<h2>"for" loops + arrays = 🎉</h2>
<pre class="fragment"><code class="javascript" data-trim>
const students = ["Marry","Barb","Phil"];
// Loops through array and console.logs each item
for(let i = 0; i < students.length; i++){
const student = students[i]; // iterator, i, used as index
console.log(student);
}
</code></pre>
</section>
<section>
<h2>"while" loop</h2>
<p>Works much like the "for" loop, but the 3 parts (initialization, condition, and final expression) are
delegated differently</p>
<pre class="fragment"><code class="javascript" data-trim>
const students = ["Marry","Barb","Phil"];
// Loops through array and console.logs each item
for(let i = 0; i < students.length; i++){
const student = students[i]; // iterator, i, used as index
console.log(student);
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const students = ["Marry","Barb","Phil"];
let i = 0; // initialization OUTSIDE of "while" loop
while(i < students.length){ // condition - eventually must evalauate to false!
const student = students[i];
console.log(student);
i++; // final expression
}
</code></pre>
</section>
<section>
<h2>Conditionals</h2>
<ul>
<li class="fragment">Conditional statements allow for code to be differentially executed based on specific
conditions</li>
<li class="fragment">"if" and "if/else" statements are the most common conditional statements</li>
</ul>
<pre class="fragment"><code class="javascript" data-trim>
const age = 23;
if(age > 18){
console.log("Welcome to the casino!");
}else{
console.log("YOU SHALL NOT PASS!");
}
</code></pre>
</section>
<section>
<h2>Logical Operators</h2>
<p class="fragment">Just like math operators, logical operators allow us to govern how logical expressions are
evaluated and will always evaluate to a boolean</p>
<pre class="fragment"><code class="javascript" data-trim>
5 > 4; // true
12 < 6; // false
16 >= 4; // true
3 <= 7; // true
"this" == "that"; // false (checks equality)
"GDI" != "GDI"; // false (checks inequality)
</code></pre>
</section>
<section>
<h2>Boolean Operators</h2>
<p class="fragment">Boolean operators allow us to combine logical expressions in more complex ways</p>
<pre class="fragment"><code class="javascript" data-trim>
// ||
// OR - returns true if one side evaluates to true
true || true; // true
true || false; // true
false || true; // true
false || false; // false
// &&
// AND - returns true if BOTH sides evaluate to true
true && true; // true
true && false; // false
false && true; // false
false && false; // false
// !
// NOT - negates the logical expressions that follows
!true; // false
!false; // true
</code></pre>
</section>
<section>
<h2>Truthy vs. Falsey</h2>
<ul class="fragment">
<li>Values that can be coerced into <code>true</code> are "truthy"</li>
<li>Values that can be coerced into <code>false</code> are "falsey"</li>
</ul>
<br />
<br />
<p class="fragment">There are fewer "falsey" values, so let's define them and consider everything else as
"truthy"</p>
<pre class="fragment"><code class="javascript" data-trim>
if (false) // actual false
if (null)
if (undefined) // such as a variable defined unassigned variable
let x; // undefined by default
if (x)
if (0) // zero!
if (NaN) // Not A Number, can happen if you add undefined to a number
if ("") // empty string
</code></pre>
</section>
<section>
<h1>BREAK</h1>
</section>
<section>
<h2>Functions</h2>
<p class="fragment">Functions are a way to store code to be executed later</p>
<pre class="fragment"><code class="javascript" data-trim>
function coinFlip(){
// Math.random() returns a random decimal between 0 and 1
return Math.random() > .5;
}
</code></pre>
</section>
<section>
<h2>Defining Functions</h2>
<p class="fragment">MANY ways to define a function in JS. Here are a few basic ways.</p>
<pre class="fragment"><code class="javascript" data-trim>
function coinFlip(){
return Math.random() > .5;
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const coinFlip = function(){
return Math.random() > .5;
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const coinFlip = () => Math.random() > .5; // We'll dive into this later!
</code></pre>
</section>
<section>
<h2>Functions with parameters</h2>
<p class="fragment">Functions can accept arguments, or information passed into them when they are called</p>
<pre class="fragment"><code class="javascript" data-trim>
function getTotalPrice(price, tax){
return price * (1 + tax);
}
getTotalPrice(100, .08); // 108
</code></pre>
</section>
<section>
<h2>Let's Develop It!</h2>
<p>In the "mainProject" folder, go into "class_1", and edit the "script.js" file</p>
<ul>
<li>Let's psuedocode this together!</li>
<li>Create a function "randomItem" that takes 1 argument: an array</li>
<li>The function should return a random item from the given array</li>
</ul>
<div class="fragment">
<p>HINT:</p>
<pre><code class="javascript" data-trim>
// Math.random() returns a random decimal in the range of 0 and 1
Math.random(); // 0.231657 a random number in the range of 0 and 1
// You can scale up a range through multiplication
const val = Math.random() * 12; // 5.632467 a random number in the range of 0 and 12
// Math.floor() removes the decimal part of a number
Math.floor(val); // 5
</code></pre>
</div>
</section>
<section>
<h2>One possible solution...</h2>
<pre class="fragment"><code class="javascript" data-trim>
const randomItem = function(arr){
const randDecimal = Math.random(); // decimal b/t 0 and 1
const randNumber = randDecimal * arr.length; // number b/t 0 and arr.lenth
const randIndex = Math.floor(randNumber); // whole number b/t 0 and arr.length
return arr[randIndex];
}
</code></pre>
</section>
<section>
<h2>Ternary Operator</h2>
<p class="fragment">The ternary operator is another way of representing an "if/else" statement in one line</p>
<pre class="fragment"><code class="javascript" data-trim>
const user_authenticated = false;
let message;
if(user_authenticated){
message = "Welcome to your homepage!";
}else{
message = "Access Denied!";
}
console.log(message); // "Access Denied!";
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const user_authenticated = false;
let message = user_authenticated ? "Welcome to your homepage!" : "Access Denied!";
console.log(message); // "Access Denied!";
</code></pre>
<p class="fragment">NOTE: it's best practice to only use the ternary operator when the logic and values are
simple</p>
</section>
<section>
<h2>Arrow Function</h2>
<p>Similar to the ternary operator, the arrow function helps keep simple functions succinct and on 1 line</p>
<pre class="fragment"><code class="javascript" data-trim>
const seasonOfLove = function(){
return 525600;
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const seasonOfLove = () => 525600;
// No parameters, therefore use empty parentheses
// One line, no need for brackets
// and automatically returns everything after the arrow
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const minutesFromYears = num_of_years => num_of_years * 525600;
// With 1 parameter, can just use parameter without parentheses
const add = (num1,num2) => num1 + num2;
// With more parameters, need parentheses
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const minutesFromYears = num_of_years => {
const leapDays = Math.floor(num_of_years / 4);
return (num_of_years * 525600) + leapDays;
};
// With larger blocks of code, can use multiple lines,
// brackets, and an explicit return statement
</code></pre>
</section>
<section>
<h2>Let's Develop It!</h2>
<p>In the "mainProject" folder, go into "class_1", and edit the "script.js" file</p>
<ul>
<li class="fragment">Let's psuedcode the "randomizeArray" function together!</li>
<li class="fragment">Work in pairs to create the function</li>
</ul>
<div class="fragment">
<p>HINT:</p>
<pre><code class="javascript" data-trim>
const students = ["Billie","Jean","Izzinot Mye"];
// .push() adds an item to the end of the array.
students.push("Love R.");
console.log(students); // ["Billie", "Jean", "Izzinot Mye", "Love R."]
// .splice() takes 2 arguments: an index and a count.
// Items are removed from the array starting at the given index
// Removal stops after the count is reached.
// The removed items are returned in an array.
const removed = students.splice(2,1); // Starting at index 2, remove 1 item
console.log(removed); // ["Izzinot Mye"] <- It's an array!
console.log(students); // ["Billie", "Jean", "Love R."]
</code></pre>
</div>
</section>
<section>
<h2>Take Home Assignment</h2>
<ul>
<li class="fragment">Finish the tasks in the "script.js" file</li>
<li class="fragment">Test to make sure the "randomizeArray" function works properly - we'll be using this
function for the rest of the course!</li>
<li class="fragment">Tackle as many bonuses as you can!</li>
</ul>
</section>
<section>
<h1>Additional Resources</h1>
<ul>
<li>
<a target="_blank" href="https://websitesetup.org/wp-content/uploads/2018/04/wsu-js-cheat-sheet.pdf">JavaScript Cheat Sheet</a>
</li>
<li>
<a target="_blank" href="https://www.w3schools.com/js/js_array_methods.asp">Array Methods</a>
</li>
<li><a href="https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26" target="_blank">Arrow Function - blog</a></li>
<li><a href="https://www.youtube.com/watch?v=mrYMzpbFz18" target="_blank">Arrow Function - video</a></li>
</ul>
</section>
<section>
<h2>"while" loop trickery</h2>
<p>Since each part of the loop is separate, we can often get away with more succinct code</p>
<pre class="fragment"><code class="javascript" data-trim>
const students = ["Marry","Barb","Phil"];
while(students.length){ // 0 equals false
const student = students.shift(); // removes first item from array
console.log(student);
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
const students = ["Marry","Barb","Phil"];
while(students.length) console.log(students.shift());
// No need for brackets when all on one line
</code></pre>
<p class="fragment">WARNING: Beware the infinite "while" loop where the condition is always true!</p>
</section>
</div>
</div>
<footer>
<div class="copyright">
Intermediate JS - GDINYC
<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>