forked from girldevelopit/gdi-featured-js-intro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class2.html
480 lines (410 loc) · 18.5 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 2 - Intro to JavaScript - Girl Develop It</title>
<meta name="description" content="Girl Develop It framework for easily creating beautiful presentations using HTML in GDI theme. Forked from Hakim El Hattab's reveal.js">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="dist/css/reveal.css">
<link rel="stylesheet" href="dist/css/default.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor --> <link rel="stylesheet" href="dist/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 = '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>
<div class="reveal">
<div class="slides">
<section>
<img src="dist/img/circle-gdi-logo.png" alt="GDI Logo" class="noborder" style="max-height:400px;">
<h1>Introduction to JavaScript</h1>
<h2>Class 2</h2>
</section>
<!-- Review -->
<section>
<h3>Let's Look Back</h3>
<img src="dist/img/backwards-dance.jpg" alt="Woman looking backward as she dances"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/zeepack/4398082292/" target="_blank">Deepak Bhatia</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Code Search</h3>
<p>In this code, spot the comment, variables, and operators.</p>
<pre><code class="javascript">var billPreTip = 10;
var tipPercent = 0.15; // Can be changed
var billTip = billPreTip * tipPercent;
var receipt = 'Meal: ' + billPreTip + ' Tip: ' + billTip +
' Total: ' + (billPreTip + billTip);
console.log(receipt);
</code></pre>
</section>
<!-- Functions -->
<section>
<h3>Functions</h3>
<p>Functions are separable, reusable pieces of code.
<img src="dist/img/legos.jpg" alt="Pile of legos"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/catzrule/9940388305/">Rick Payette</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>Declaring Functions</h3>
<p>To declare (create) a function, you can give it a name, then include all the code for the function inside curly brackets <code>{}</code></p>
<pre><code class="javascript">function parrotFacts() {
console.log('Some parrot species can live for over 80 years');
console.log('Kakapos are a critically endangered flightless parrot');
}
</code></pre>
<p>Functions can have multiple lines.</p>
</section>
<section>
<h3>Using Functions</h3>
<p>To invoke (use) a function, you type its name, followed by parenthesis <code>()</code></p>
<pre><code class="javascript"> parrotFacts();</code></pre>
<p>We'll talk about what can go inside those parenthesis in a minute! For now, leave them empty.</p>
</section>
<section>
<h3>What's going on here?</h3>
<p>A function is a group of code you can reuse many times. Whenever you invoke a function by using it's name, you tell the browser to run the code inside the function.</p>
<p>You must declare a function before you can use it.</p>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Write a function that outputs a sentence. Then invoke that function later in your code.</p>
</section>
<section>
<h3>Arguments</h3>
<p>Functions can accept input values, called <code>arguments</code>.</p>
<pre><code class="javascript">function callKitten(kittenName){
console.log('Come here, ' + kittenName + '!');
}
callKitten('Fluffy'); // outputs 'Come here, Fluffy!'
function addNumbers(a, b){
console.log(a + b);
}
addNumbers(5, 7); // outputs 12
addNumbers(9, 12); // outputs 21
</code></pre>
</section>
<section>
<h3>Arguments</h3>
<p>You can also pass <code>variables</code> into functions. These variables do not need to have the same name as the function arguments.</p>
<pre><code class="javascript">function addOne(num){
var newNumber = num + 1;
console.log('You now have ' + newNumber);
}
// Declare variables
var numberOfKittens = 5;
var numberOfPuppies = 4;
// Use them in functions
addOne(numberOfKittens);
addOne(numberOfPuppies);
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Write a simple program to combine a first name and a last name inside a function. Then update the function to accept a first and last name as arguments.</p>
</section>
<!-- returning values -->
<section>
<h3>Returning Values</h3>
<p>You can have a function give you back a value, to use later.</p>
<pre><code class="javascript">function square(num) {
return num * num;
}
console.log(square(4)); // outputs '16'
var squareOfFive = square(5); // squareOfFive equals '25'
</code></pre>
<p><code>return</code> will immediately end a function.</p>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Add a return statement to your 'name' function. Use that function to set the value of a variable.</p>
</section>
<!-- Variable Scope -->
<section>
<h3>Variable Scope</h3>
<p>The scope of a variable determines where it's value is accessible throughout your program.</p>
<img src="dist/img/footprints.jpg" alt="Footprints being washed away"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/heyvikram/187950111/" target="_blank">_vikram</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Global Scope</h3>
<p>A variable declared outside of a function has a <code>global scope</code> and can be accessed anywhere, even inside of functions.</p>
<pre><code class="javascript">var awesomeGroup = 'Girl Develop It'; // Global scope
function whatIsAwesome() {
console.log(awesomeGroup + ' is pretty awesome.'); // Will work
}
whatIsAwesome();
</code></pre>
</section>
<section>
<h3>Local Scope</h3>
<p>A variable declared inside a function has a <code>local scope</code> and can only be accessed within that function.</p>
<pre><code class="javascript">function whatIsAwesome() {
var awesomeGroup = 'Girl Develop It'; // Local scope
console.log(awesomeGroup + ' is pretty awesome.'); // Will work
}
whatIsAwesome();
console.log(awesomeGroup + ' is pretty awesome.'); // Won't work
</code></pre>
</section>
<!-- Boolean -->
<section>
<h3>Boolean Variables</h3>
<img src="dist/img/kitten-switch.jpg" alt="Kitten with light switch"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/elycefeliz/5726163170/" target="_blank">elycefeliz</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Boolean Variables</h3>
<p>Boolean variables represent the logical values <code>true</code> and <code>false</code></p>
<pre><code class="javascript">var catsAreBest = true;
var dogsRule = false;
</code></pre>
</section>
<section>
<h3>Boolean Variables</h3>
<p>Some values are considered <code><a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">falsy</a></code> and will evaluate to <code>false</code> in a Boolean context.</p>
<pre><code class="javascript">// the following variables will evaluate as false
var myName = '';
var numOfKids = 0;
var isMarried; // remember a variable with no value is undefined</code></pre>
<p><code>null</code> and <code>NaN</code> will also evaluate as <code>false</code>.</p>
<p>Everything else evaluates as <code>true</code>.</p>
</section>
<!--If/Then-->
<section>
<h3>Control Flow</h3>
<img src="dist/img/fork-path.jpg" alt="Forked path"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/dennism2/1504087870/" target="_blank">DennisM2</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>The if statement</h3>
<p>Use <code>if</code> statements to decide which lines of code to execute, based on a condition.</p>
<pre><code class="javascript">if (condition) {
// statements to execute
}
</code></pre>
<pre><code class="javascript">var age = 30;
if (age > 18) {
console.log('You are an adult');
}</code></pre>
</section>
<section>
<h3>Comparison Operators</h3>
<table class="smalltext">
<thead>
<tr>
<th>Example</th>
<th>Name</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>a == b</code></td>
<td>Equal</td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is equal to <code>b</code> (can be different types).</td>
</tr>
<tr>
<td><code>a === b</code></td>
<td>Identical</td>
<td>
<strong><code>TRUE</code></strong> if <code>a</code> is equal to <code>b</code>, and the same type. </td>
</tr>
<tr>
<td><code>a != b</code></td>
<td>Not equal</td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is not equal to <code>b</code> (can be different types).</td>
</tr>
<tr>
<td><code>a !== b</code></td>
<td>Not identical</td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is not equal to <code>b</code>, or they are not the same type.</td>
</tr>
<tr>
<td><code>a < b</code></td>
<td>Less than</td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is strictly less than <code>b</code>.</td>
</tr>
<tr>
<td><code>a > b</code></td>
<td>Greater than</td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is strictly greater than <code>b</code>.</td>
</tr>
<tr>
<td><code>a <= b</code></td>
<td>Less than or equal to </td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is less than or equal to <code>b</code>.</td>
</tr>
<tr>
<td><code>a >= b</code></td>
<td>Greater than or equal to </td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is greater than or equal to <code>b</code>.</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3>Watch out!</h3>
<p>Don't mix up <code>=</code> and <code>==</code> and <code>===</code></p>
<img src="dist/img/caution.jpg" alt="Caution Tape"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/pictureperfectpose/76138988/" target="_blank">Eugene Zemlyanskiy</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Make a variable called "temperature". Write some code that tells you to put on a coat if it is below 50 degrees.</p>
</section>
<!--If/else -->
<section>
<h3>Even more control flow</h3>
<img src="dist/img/toomanysigns.jpg" alt="Sign post with multiple signs"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/thomashawk/2402598275/" target="_blank">Thomas Hawk</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>The if/else statement</h3>
<p>Use <code>else</code> to provide an alternate set of instructions.</p>
<pre><code class="javascript">var age = 30;
if (age >= 16) {
console.log('Yay, you can drive!');
} else {
console.log('Sorry, you have ' + (16 - age) +
' years until you can drive.');
}
</code></pre>
</section>
<section>
<h3>The if/else statement</h3>
<p>If you have multiple conditions, you can use <code>else if</code>.</p>
<pre><code class="javascript">var age = 30;
if (age >= 35) {
console.log('You can vote AND run for President!');
} else if (age >= 30) {
console.log('You can vote AND run for the Senate!');
} else if (age >= 18) {
console.log('You can vote!');
} else {
console.log('You can\'t vote, but you can write your representatives.');
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Modify your "wear a coat" code for these conditions:</p>
<ol>
<li>If it's less than 50 degrees, wear a coat.</li>
<li>If it's less than 30 degrees, wear a coat and a hat.</li>
<li>If it's less than 0 degrees, stay inside.</li>
<li>Otherwise, wear whatever you want.</li>
</ol>
</section>
<!-- Logical Operators -->
<section>
<h3>Logical Operators</h3>
<table>
<thead>
<tr>
<th>Example</th>
<th>Name</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>a && b</code></td>
<td>And</td>
<td><strong><code>TRUE</code></strong> if both <code>a</code> and <code>b</code> are <strong><code>TRUE</code></strong>.</td>
</tr>
<tr>
<td><code>a || b</code></td>
<td>Or</td>
<td><strong><code>TRUE</code></strong> if either <code>a</code> or <code>b</code> is <strong><code>TRUE</code></strong>.</td>
</tr>
<tr>
<td><code>!a</code></td>
<td>Not</td>
<td><strong><code>TRUE</code></strong> if <code>a</code> is not <strong><code>TRUE</code></strong>.</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3>Using logical operators</h3>
<p>You can use these operators to combine conditions.</p>
<pre><code class="javascript">var age = 30;
var yearsAsCitizen = 30;
if (age >=30 && yearsAsCitizen > 9) {
console.log('You can run for the Senate!');
} else {
console.log('You are not eligible to run for the Senate');
}</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Add a logical operator to your "what to wear" program.</p>
</section>
<!-- Final slides-->
<section>
<h3>You did it!</h3>
<img src="dist/img/kitten-yay.jpg" alt="Happy cat"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/laura-kali/7399310732/" target="_blank">OnceAndFutureLaura</a> <a href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Resources</h3>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide" target="_blank">JavaScript Guide</a>, from the Mozilla Developers Network.</li>
<li><a href="http://www.codecademy.com/tracks/javascript" target="_blank">Code Academy</a>, with interactive JavaScript lessons to help you review.</li>
</ul>
</section>
<section>
<h2>Questions?</h2>
</section>
</div>
<footer>
<div class="copyright">
Intro to JavaScript — Girl Develop It —
<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="dist/js/head.min.js"></script>
<script src="dist/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>