-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass3.html
549 lines (503 loc) · 21.6 KB
/
class3.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 3 ~ 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">
<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="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 3</h3>
</section>
<section>
<h1>Agenda</h1>
<ul>
<li>Review Quiz</li>
<li>Asynchronous JS</li>
<li>API's</li>
<li>fetch()</li>
</ul>
</section>
<section>
<section>
<h1>Review Quiz</h1>
<h3>⬇</h3>
</section>
<section>
<p>What is wrong with the following function?</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
function factorial(n) => {
if (n===1) {
return 1;
}
return n * factorial(n-1);
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
// It's trying to combine 2 different ways of defining a function
// Could go with function declaration...
function factorial(n) {
if (n===1) {
return 1;
}
return n * factorial(n-1);
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
// ...or arrow function
const factorial = n => {
if (n===1) {
return 1;
}
return n * factorial(n-1);
}
</code></pre>
</section>
<section>
<p>How would you access the 2nd item in the object's array?</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const superman = {
name: "Kal-El",
alter_ego: "Clark Kent",
origin: "Krypton",
powers: ["strength","x-ray vision","indestructibility","ice breath","eye beam"]
}
</code></pre>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
superman.powers[1]; // x-ray vision
</code></pre>
</section>
<section>
<p>I can't see the <p> element I added to the DOM. Here's my "script.js" file. Why isn't it working?😭
</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const container = document.querySelector(".container");
const p = document.createElement("p");
p.innerHTML = "Hello World!";
</code></pre>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
// Need to attach/append the newly create element to a DOM element
container.append(p);
</code></pre>
</section>
<section>
<p>I made the change - my JavaScript is correct, but I still can't see the <p> element on the website. Here's my
"index.html" file. Why isn't it working?😭</p>
<pre class="fragment"><code class="xml" data-trim contenteditable>
<html>
<head>
<title>Cool Site</title>
</head>
<body>
<div class="container"></div>
</body>
</html>
</code></pre>
<pre class="fragment"><code class="xml" data-trim contenteditable>
<html>
<head>
<title>Cool Site</title>
<!--Forgot to load the JavaScript file!-->
<script src="script.js"></script>
</head>
<body>
<div class="container"></div>
</body>
</html>
</code></pre>
</section>
</section>
<section>
<h2>Synchronous JavaScript</h2>
<p class="fragment">JavaScript executes each line of code from top to bottom</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const name = "Bob";
const age = 42;
console.log(name); // Bob
console.log(age); // 42
</code></pre>
<p class="fragment">This is a common and expected behavior of code execution</p>
</section>
<section>
<h2>Asynchronous Real Life</h2>
<ul>
<li class="fragment">Your left in charge of the house and your mom has left you a TO-DO list:
<ol>
<li>Schedule vet appt.</li>
<li>Take out garbage</li>
<li>Wash the dishes</li>
</ol>
</li>
<li class="fragment">You're very particular and want to do everything in order (like JS!)</li>
<li class="fragment">When you call the vet, you're put on hold...</li>
<li class="fragment">You're not going to just wait around! While on hold, you take out the garbage and wash the dishes</li>
<li class="fragment">Eventually, the vet comes back and you schedule the appt.</li>
</ul>
</section>
<section>
<h2>Asynchronous JavaScript</h2>
<p class="fragment">There are instances when code will get executed, but NOT resolve immediately, such
as requesting information from an <span class="emph">API</span> server</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const URL = "https://randomuser.me/api";
let data;
fetch(URL) // Requesting JSON from API (like $.ajax)
.then(response=>response.json())
.then(json=>{
data=json;
})
console.log(data); // undefined
</code></pre>
<p class="fragment">During code execution, the fetch() call sends a request to a URL, but that could take
a long time (~6ms)! JavaScript is not going to wait - it will move onto the console.log() and let <span
class="emph">callbacks</span> handle
the response</p>
</section>
<section>
<h2>Let's Develop It! (Instructor only)</h2>
<pre><code class="javascript" data-trim contenteditable>
const URL = "https://randomuser.me/api";
let data;
fetch(URL) // Requesting JSON from API (like $.ajax)
.then(response=>response.json())
.then(json=>{
data=json;
})
console.log(data); // undefined
</code></pre>
<ul>
<li>Run the previous slides' code in the browser</li>
<li>console.log(data) again after a few seconds</li>
<li>What's happening?</li>
</ul>
</section>
<section>
<h2>API's</h2>
<p class="fragment">API stands for Application Program Interface</p>
<ul>
<li class="fragment">API's allow computers to communicate with other computers, usually through JSON</li>
<li class="fragment">For Example, the <a target="_blank" href="https://randomuser.me/api">Random User API</a>
returns JSON of a
typical user as an object with random properties like name, address, passwords, and phone numbers - it's
also a
great introductory API!</li>
</ul>
<br>
<br>
<div class="fragment">
<p>Example JSON from Random User API</p>
<pre><code class="javascript" data-trim contenteditable>
{"results":[{"gender":"female","name":{"title":"ms","first":"carolyn","last":"brooks"},"location":{"street":"5508 king street","city":"wakefield","state":"warwickshire","postcode":"M0 5UX","coordinates":{"latitude":"14.0309","longitude":"165.8724"},"timezone":{"offset":"+8:00","description":"Beijing, Perth, Singapore, Hong Kong"}},"email":"[email protected]","login":{"uuid":"31cd84ee-8a01-4254-b322-675ef00de0fa","username":"bluegorilla497","password":"birthday","salt":"Nbhjusmn","md5":"6bc053eccfdc3078acab5d96b5278f0f","sha1":"371147ed185f134a65e0f201b0f82b5e55ff2223","sha256":"60fc47a547217783428ffc170709adc8bad0efa5404fff41282c57d9b37c33ba"},"dob":{"date":"1962-08-29T20:41:30Z","age":56},"registered":{"date":"2016-12-08T04:51:11Z","age":2},"phone":"028 3843 5424","cell":"0770-555-954","id":{"name":"NINO","value":"LH 29 72 52 O"},"picture":{"large":"https://randomuser.me/api/portraits/women/95.jpg","medium":"https://randomuser.me/api/portraits/med/women/95.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/95.jpg"},"nat":"GB"}],"info":{"seed":"ed31d9a21fc2e60e","results":1,"page":1,"version":"1.2"}}
</code></pre>
</div>
</section>
<section>
<h2>A cautionary tale...</h2>
<ul>
<li class="fragment">Typically, API servers are set up to protect themselves from excessive requests from a
single source, or IP address</li>
<li class="fragment">When too many requests come in, the server will often blacklist the IP address for 15
minutes or more</li>
<li class="fragment">This is why the last activity was just for the instructor!</li>
<li class="fragment">In the later activities, if your wifi's IP address is blacklisted - just use the backup data
provided as a substitute</li>
</ul>
</section>
<section>
<h2>Let's Develop It!</h2>
<ul>
<li>In your browser, go to the url - <a href="https://randomuser.me/api"
target="_blank">https://randomuser.me/api</a></li>
<li>Open the browser's console and store the JSON (copy and paste it) in a variable "json"</li>
<li>How would you access the following properties:
<ul>
<li class="fragment">cell number?</li>
<li class="fragment">username?</li>
<li class="fragment">API version?</li>
</ul>
</li>
</ul>
<br>
<br>
<div class="fragment">
<p>Backup JSON from Random User API - just in case!</p>
<pre><code class="javascript" data-trim contenteditable>
{"results":[{"gender":"female","name":{"title":"ms","first":"carolyn","last":"brooks"},"location":{"street":"5508 king street","city":"wakefield","state":"warwickshire","postcode":"M0 5UX","coordinates":{"latitude":"14.0309","longitude":"165.8724"},"timezone":{"offset":"+8:00","description":"Beijing, Perth, Singapore, Hong Kong"}},"email":"[email protected]","login":{"uuid":"31cd84ee-8a01-4254-b322-675ef00de0fa","username":"bluegorilla497","password":"birthday","salt":"Nbhjusmn","md5":"6bc053eccfdc3078acab5d96b5278f0f","sha1":"371147ed185f134a65e0f201b0f82b5e55ff2223","sha256":"60fc47a547217783428ffc170709adc8bad0efa5404fff41282c57d9b37c33ba"},"dob":{"date":"1962-08-29T20:41:30Z","age":56},"registered":{"date":"2016-12-08T04:51:11Z","age":2},"phone":"028 3843 5424","cell":"0770-555-954","id":{"name":"NINO","value":"LH 29 72 52 O"},"picture":{"large":"https://randomuser.me/api/portraits/women/95.jpg","medium":"https://randomuser.me/api/portraits/med/women/95.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/95.jpg"},"nat":"GB"}],"info":{"seed":"ed31d9a21fc2e60e","results":1,"page":1,"version":"1.2"}}
</code></pre>
</div>
</section>
<section>
<h2>Let's Develop It! - Answers</h2>
<pre><code class="javascript" data-trim contenteditable>
json.results[0].cell;
json.results[0].login.username;
json.info.version;
</code></pre>
</section>
<section>
<h2>Callbacks</h2>
<p class="fragment">A callback is a function that is passed into another function as an argument to be called later</p>
<ul>
<li class="fragment">Usually, we define functions and call them directly</li>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const add = (a,b) => a + b;
add(1,3); // 4
</code></pre>
<li class="fragment">However, functions are variables, too, and can be passed as arguments!</li>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const numbers = [1,2,3];
combineArray(numbers, add); // We'll define "combineArray" in a sec!
</code></pre>
</ul>
<p class="fragment">NOTE: when passing a callback, do NOT use parenthesis, which would immediately invoke the function</p>
</section>
<section>
<h2>Callbacks</h2>
<ul>
<li class="fragment">The function being passed as an argument is known as a callback</li>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const add = (a,b) => a + b;
/**
* Combines each element of the given array
* using the given combiningFunction.
*/
const combineArray = (arr, combiningFunction) => {
let total = arr[0];
for(let i = 1; i < arr.length; i++){
total = combiningFunction(total, arr[i]); // callback called here
}
return total;
}
const numbers = [1,2,3];
combineArray(numbers, add); // 6
</code></pre>
</ul>
</section>
<section>
<h2>Callbacks</h2>
<ul>
<li class="fragment">The function recieving the callback is known as a higher-order function</li>
<li class="fragment">Higher-order functions separate logic and allow developers to determine how they want to handle code execution</li>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const stringify = (a,b) => String(a) + String(b);
/**
* Combines each element of the given array
* using the given combiningFunction.
*/
const combineArray = (arr, combiningFunction) => {
let total = arr[0];
for(let i = 1; i < arr.length; i++){
total = combiningFunction(total, arr[i]); // callback called here
}
return total;
}
const numbers = [1,2,3];
combineArray(numbers, stringify); // "123"
</code></pre>
</ul>
</section>
<section>
<h1>BREAK</h1>
</section>
<section>
<h2>Using fetch()</h2>
<p class="fragment">fetch() is a default function in the browser that can make HTTP requests</p>
<ul>
<li class="fragment">fetch() takes a URL string, which it uses to make the request</li>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const URL = "https://randomuser.me/api";
fetch(URL);
</code></pre>
<li class="fragment">A Promise object is returned, which has a .then() method</li>
<li class="fragment">.then() takes a callback and passes a single argument to the callback!</li>
</ul>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const URL = "https://randomuser.me/api";
const handleResponse = response => {
// do something with response
}
fetch(URL).then(handleResponse);
</code></pre>
</section>
<section>
<h2>Let's Develop It!</h2>
<pre><code class="javascript" data-trim contenteditable>
const URL = "https://randomuser.me/api";
const handleResponse = response =>{
console.log(response);
}
fetch(URL).then(handleResponse);
</code></pre>
<p>Run the above code in the browser</p>
<ol style="font-size:28px;">
<li class="fragment">What is passed into the "handleResponse" callback?</li>
<li class="fragment">Explore the Response object and find the .json() method under __proto__</li>
<li class="fragment">Call the .json() method within the console.log() - what does it return?</li>
<li class="fragment">Have the "handleResponse" return response.json()</li>
<li class="fragment">Attach another .then() to the previous .then() and provide the function "handleJSON" as its argument</li>
<li class="fragment">Define the "handleJSON" function - console.log() the data passed into it from the second .then()</li>
</ol>
</section>
<section>
<h2>Let's Develop It! - Result</h2>
<pre><code class="javascript" data-trim contenteditable>
const URL = "https://randomuser.me/api";
const handleResponse = response => response.json();
const handleJSON = json => console.log(json);
fetch(URL).then(handleResponse).then(handleJSON);
</code></pre>
<p>The "handleJSON" function should console.log() the same object you can see when going directly to <a href="https://randomuser.me/api" target="_blank">https://randomuser.me/api</a> in the browser</p>
</section>
<section>
<h2>Remember from earlier...</h2>
<pre><code class="javascript" data-trim contenteditable>
const URL = "https://randomuser.me/api";
let data;
fetch(URL)
.then(response=>response.json())
.then(json=>{
data = json; // setting data!
})
console.log(data); // undefined...wait, what!?
</code></pre>
<ul>
<li class="fragment">The JSON from the response CANNOT be accessed synchronously</li>
<li class="fragment">The JSON only exists within the "handleJSON" callback - so it's the only place we can actually make use of it!</li>
</ul>
</section>
<section>
<h2>Let's Develop It!</h2>
<p>In the "mainProject" folder, go into "class_3", and edit the "script.js" file</p>
<ul>
<li>Follow the instructions for making the "addQuestions" function and modifying some previous code</li>
<li>Be sure to explore what data the API sends back and get a good grasp of the flow of information</li>
</ul>
</section>
<section>
<h2>Take Home Assignment</h2>
<ul>
<li>Finish all of the tasks in the "script.js" file - we'll be using it for the last class!</li>
<li>Practice using one of the following APIs
<ol>
<li><a href="https://dog.ceo/dog-api/">Dog API - https://dog.ceo/dog-api/</a></li>
<li><a href="http://deckofcardsapi.com/">Deck of Cards API - http://deckofcardsapi.com/</a></li>
<li><a href="https://pokeapi.co/">PokéAPI - https://pokeapi.co/</a></li>
</ol>
</li>
<li>Tackle as many bonuses as you can!</li>
</ul>
</section>
<section>
<h1>Additional Resources</h1>
<ul>
<li><a href="https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en" target="blank">CORS - Chrome Extension</a></li>
<li><a href="https://www.youtube.com/watch?v=QO4NXhWo_NM" target="blank">JS Promises by Daniel Shiffman</a></li>
<li><a href="https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw" target="blank">The Coding Train - Daniel Shiffman</a></li>
<li><a href="https://davidwalsh.name/fetch" target="blank">Fetch by David Walsh</a></li>
<li><a href="https://davidwalsh.name/" target="blank">David Walsh - Blog</a></li>
</ul>
</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>