-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass4.html
490 lines (439 loc) · 17.7 KB
/
class4.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 4 ~ 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 4</h3>
</section>
<section>
<h1>Agenda</h1>
<ul>
<li>Review Quiz</li>
<li>Quiz App: last steps</li>
<li>Other APIs</li>
<li>Goodbye!😢</li>
</ul>
</section>
<section>
<section>
<h1>Review Quiz</h1>
<h3>⬇</h3>
</section>
<section>
<p>What could be improved in the following code?</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
let students = ["Bob","Amy","Betty","Bill","Marge","Charlotte","Shawn","Pedro"];
let longestName = function(){
let output = "";
for(let i = 0; i < students.length; i++){
let name = students[i];
if(name.length > output.length){
output = name;
}
}
return output;
}
longestName();
</code></pre>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
// preference for const
const students = ["Bob","Amy","Betty","Bill","Marge","Charlotte","Shawn","Pedro"];
// Keep array reference within function as parameter (more versatile)
const longestStr = function(arr){
let output = "";
for(let i = 0; i < arr.length; i++){
const str = arr[i];
output = str.length > output.length ? str : output;
}
return output;
}
longestStr(students); // must pass data into fun ction as argument
</code></pre>
</section>
<section>
<p>What is the value of pikachu.health?</p>
<pre class="fragment"><code class="javascript" data-trim>
const pikachu = {
health: 10,
defense: 5
}
const charmander = {
punch: function(obj){
const damage = 12 - obj.defense;
obj.health -= damage;
}
}
charmander.punch(pikachu);
</code></pre>
<pre class="fragment"><code class="javascript" data-trim>
pikachu.health; // (10 - (12 - 5)) == (10 - 7) == 3
</code></pre>
</section>
<section>
<p>How would you get all divs with a class of "xyz" from the DOM?</p>
<pre class="fragment"><code class="javascript" data-trim>
const divs = document.querySelectorAll("div.xyz");
</code></pre>
</section>
<section>
<p>Why does the following console.log() print "undefined?"</p>
<pre class="fragment"><code class="javascript" data-trim>
const URL = "https://some-api.com/api";
let info;
fetch(URL)
.then(r=>r.json())
.then(data=>{
info = data;
})
console.log(info); // undefined
</code></pre>
<p class="fragment">The asynchronous fetch() call does not reassign the "info" variable before the console.log() executes</p>
</section>
</section>
<section>
<h2>Events</h2>
<p class="fragment">An events is some notable moment for an element, such as when a user:</p>
<ul>
<li class="fragment">scrolls on the page</li>
<li class="fragment">clicks an elements</li>
<li class="fragment">presses a certain key</li>
</ul>
<br>
<br>
<p class="fragment">Check out <a href="https://developer.mozilla.org/en-US/docs/Web/Events" target="_blank">all of the events</a> that can occur on a website</p>
</section>
<section>
<h2>Event Handlers</h2>
<p class="fragment">An event handler is a callback function that can be assigned to an element to <strong>trigger</strong> when an event occurs</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const button = document.querySelector("button");
button.addEventListener("click",handleClick);
function handleClick(event){
// event is an Event object with information about the event
alert("Don't press me!");
}
</code></pre>
</section>
<section>
<h2>Let's Develop It!</h2>
<p>In the "mainProject" folder, go into "class_4", and edit the "script.js" file</p>
<ul>
<li>Finish up last tasks!</li>
</ul>
<pre><code class="javascript" data-trim contenteditable>
const button = document.querySelector("button");
button.addEventListener("click",handleClick);
function handleClick(event){
// event is an Event object with information about the event (check it out!)
alert("Don't press me!");
}
</code></pre>
</section>
<section>
<h2>The final product</h2>
<p><a href="mainProject/class_4/index_SOLUTION.html" target="_blank">This</a> is what we made!</p>
<img src="img/quiz_app.PNG" alt="">
</section>
<section>
<h1><br/></h1>
</section>
<section>
<h2>Working with other APIs</h2>
<p class="fragment">Not all APIs are created equal - there are 3 main ways to access an API</p>
<ol>
<li class="fragment">Open to all - no authentication required!</li>
<li class="fragment">Requires developer account and authentication key...
<ol>
<li class="fragment">which can be part of the request URL</li>
<li class="fragment">which must go in the request header</li>
</ol>
</li>
</ol>
</section>
<section>
<h2>APIs open to all</h2>
<p class="fragment">Anyone can use these without needing permission or authentication!</p>
<ul>
<li class="fragment">💲Free!💲</li>
<li class="fragment">Usually has a low limit on the number of requests that can be made from the same IP address</li>
<li class="fragment">A server is costly to maintain, and without proper backing many Open APIs shut down - making them less reliabile</li>
</ul>
</section>
<section>
<h2>APIs open to all - Example</h2>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const URL = "http://taco-randomizer.herokuapp.com/random/"; // TacoFancy API
fetch(URL)
.then(response=>response.json())
.then(console.log)
</code></pre>
</section>
<section>
<h2>APIs with key in URL</h2>
<ul>
<li class="fragment">Usually free!</li>
<li class="fragment">Must sign up for a developer account - may include a small questionarre explaining your project</li>
<li class="fragment">You will get an API key, which ensures API requests are coming from a legitimate source and not being abused</li>
<li class="fragment">API key is passes along as a URL parameter</li>
<li class="fragment"><i>You may be asked to provide the domain from which requests will come from - this is another security measure</i></li>
<li class="fragment">The request limit is usually NOT done by IP address - instead the number of requests per unit of time is limited (pay $$ to increase this limit)</li>
</ul>
</section>
<section>
<h2>APIs with key in URL - Example</h2>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const BASE_API = "https://api.nasa.gov/planetary/apod"; // NASA API
const API_KEY = "YOUR KEY HERE"; // get API key by signing up as a developer
const URL = BASE_API + "?api_key=" + API_KEY;
// "?api_key=" sets a URL parameter (can test this URL in browser directly!)
// parameter name is specified in the documentation
fetch(URL)
.then(response=>response.json())
.then(console.log)
</code></pre>
<p class="fragment"><a href="http://www.ronstauffer.com/blog/understanding-a-url/" target="_blank">Understanding a URL (with parameters)</a></p>
</section>
<section>
<h2>APIs with key in request header</h2>
<ul>
<li class="fragment">Usually free</li>
<li class="fragment">Must sign up for a developer account</li>
<li class="fragment">You will get an API key</li>
<li class="fragment">API key passed through request header - an object that accompanies the request</li>
<li class="fragment"><i>May have to provide the domain from which requests will come from</i></li>
<li class="fragment">Requests are limited by number per unit of time (pay $$ to increase this limit)</li>
</ul>
</section>
<section>
<h2>APIs with key in request header - Example</h2>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
const URL = "https://favqs.com/api/qotd"; // Fav Quotes API
const API_KEY = "YOUR KEY HERE"; // get API key by signing up as a developer
const options = { // can have MANY more options
method:"GET", // GETting response instead of POSTing one
headers: {
"Content-Type" : "application/json", // specifying JSON as returning data type
"Authorization": "Token token=" + API_KEY // specified in documentation
}
}
fetch(URL, options) // notice 2nd parameter!
.then(response=>response.json())
.then(data=>console.log(data.quote))
</code></pre>
</section>
<section>
<h2>Reading Documentation</h2>
<p class="fragment">API Documentation is written by developers to help other developers understand how to use their API</p>
<br>
<p class="fragment"><strong>Unfortunately, there is no standard for writing documentation</strong> and you will often bump into hard-to-read, outdated, and cryptic documentation</p>
</section>
<section>
<h2>Tips for Reading Documentation</h2>
<ul>
<li class="fragment">Get a sense of how the documentation is organized</li>
<li class="fragment">Explore example code (copy & paste)</li>
<li class="fragment">Try out the first 3 methods or topics that catch your eye</li>
<li class="fragment">GOOGLE IT!</li>
</ul>
</section>
<section>
<h2>Let's Develop It!</h2>
<p>Take 2 minutes to explore the <a href="https://randomuser.me/documentation" target="_blank">Random User API Documentation</a></p>
<ul>
<li class="fragment">How would you request 100 random users?</li>
<li class="fragment">How would you request 100 female random users?</li>
<li class="fragment">How would you request a male from the US?</li>
<li class="fragment">How would you request 10 random users with only their name and cell number?</li>
</ul>
</section>
<section>
<h2>Let's Develop It! - Answer</h2>
<pre><code class="javascript" data-trim>
// 100 random users
"https://randomuser.me/api/?results=100"</code></pre>
<pre><code class="javascript" data-trim>
// 100 female random users
"https://randomuser.me/api/?results=100&gender=female"</code></pre>
<pre><code class="javascript" data-trim>
// a male from the US
"https://randomuser.me/api/?gender=male&nat=us"</code></pre>
<pre><code class="javascript" data-trim>
// 10 random users with only their name and cell number
"https://randomuser.me/api/?results=10&inc=name,cell"</code></pre>
</section>
<section>
<h2>Nested API calls</h2>
<p class="fragment">A typical pattern you'll see is when the data from one API call provides the information or setup for another API call.</p>
<pre class="fragment"><code class="javascript" data-trim contenteditable>
// Random User API
const URL_1 = "https://randomuser.me/api";
// Sunrise and Sunset times API
const URL_2_BASE = "https://api.sunrise-sunset.org/json?date=today";
fetch(URL_1)
.then(response=>response.json())
.then(userData=>{
const name = userData.results[0].name;
const full_name = `${name.first} ${name.last}`;
const coordinates = userData.results[0].location.coordinates;
const lat = coordinates.latitude;
const long = coordinates.longitude;
// Sunrise & Sunset API can accept URL parameters for longitude
// and latitude to get the sunrise and sunset for that location
const URL_2 = URL_2_BASE + "&lat=" + lat + "&lng=" + long;
fetch(URL_2)
.then(response=>response.json())
.then(ss_data=>{
const sunrise = ss_data.results.sunrise;
const sunset = ss_data.results.sunset;
console.log("My name is" + full_name);
console.log("My sunrise was at " + sunrise);
console.log("My sunset will occur at " + sunset);
})
})
</code></pre>
<p class="fragment">Notice the 2nd fetch is WITHIN the first's callback</p>
</section>
<section>
<h2>Take Home Assignment</h2>
<p>Find 1 API we didn't use and create a web application allowing users to interact with it. For example:</p>
<ul>
<li>Use the Pokemon API and allow users to search for pokemon by name</li>
<li>Use the Giphy API and allow users to find images by different criteria</li>
<li>Use the Google Maps API to send users on treasure hunts based on location</li>
</ul>
<br>
<br>
<p>The possiblities are endless!</p>
</section>
<section>
<h1>Additional Resources</h1>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Events" target="blank">List of DOM Events</a></li>
<li>
<a href="https://javascript30.com/" target="_blank">JavaScript30</a>
</li>
<li><a href="https://scotch.io/" target="_blank">Scotch.io - tutorials</a></li>
<li><a href="https://github.com/toddmotto/public-apis" target="_blank">Public API List</a></li>
<li><a href="https://www.youtube.com/channel/UCEDkO7wshcDZ7UZo17rPkzQ" target="_blank">Mayuko - Vlog</a></li>
<li><a href="https://medium.com/@adrienne.tacke" target="_blank">Adrienne Tacke - Blog</a></li>
<li><a href="https://www.instagram.com/binarybeauty/" target="_blank">binarybeauty - Instagram</a></li>
<li><a href="https://twitter.com/MinaMarkham" target="_blank">Mina Markham - Twitter</a></li>
</ul>
</section>
<section>
<h2>So Long! Farewell!</h2>
<img src="img/farewell.gif" alt="">
</section>
<section>
<h2>Wrapping Up</h2>
<ul>
<li>Complete Surveys!</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>