-
Notifications
You must be signed in to change notification settings - Fork 206
/
class3.html
396 lines (330 loc) · 15.4 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 3 - 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 3</h2>
</section>
<!-- Loops -->
<section>
<h3>Loops</h3>
<img src="dist/img/hula-hoop.jpg" alt="Lego stormtrooper inside key ring"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/steve_pires/8430087504/" target="_blank">Steve Pires</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>While loops</h3>
<p><code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while">while</a></code> will repeat the same code over and over until some condition is met.</p>
<pre class="javascript"><code>var bottlesOfBeer = 99;
while (bottlesOfBeer > 0) {
console.log(bottlesOfBeer + ' bottles of beer on the wall');
bottlesOfBeer = bottlesOfBeer - 1;
}</code></pre>
</section>
<section>
<h3>Infinite Loops</h3>
<p>Make sure something changes in the loop, or your loop will go on forever...</p>
<img src="dist/img/infinite-loop.jpg" alt="Spiral that goes on toward infinity"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/samueljohn/5348462863/" target="_blank">Samuel John</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>For loops</h3>
<p><code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a></code> loops are very similar, but you declare a counter in the statement.</p>
<pre><code class="javascript">// will count 1 to 10
for (var i = 1; i <= 10; i++) {
console.log(i);
}</code></pre>
</section>
<section>
<h3>Loops and logic</h3>
<p>You can add other statements or logical operators inside the loops.</p>
<pre><code class="javascript">// Count from 1 to 100
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0) {
// Says 'Fizz' after multiples of three
console.log(' Fizz');
} else if (i % 5 === 0) {
// Says 'Buzz' after multiples of five
console.log(' Buzz');
} else {
console.log(i);
}
}</code></pre>
</section>
<section>
<h3>Break</h3>
<p>To exit a loop, use the <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break">break</a></code> statement.</p>
<pre><code class="javascript">// Count from 100 to 200
for (var i = 100; i <= 200; i++) {
console.log('Testing ' + i);
//Stop at the first multiple of 7
if (i % 7 == 0) {
console.log('Found it! ' + i);
break;
}
}</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Write a loop that gives you the 9's times table,<br /> from <strong>9 x 1 = 9</strong> to <strong>9 x 12 = 108</strong>.</p><br />
<p>Finish early? Try using a loop inside a loop to write all the times tables, from 1 to 12.</p>
</section>
<!-- Arrays -->
<section>
<h3>Arrays</h3>
<img src="dist/img/kitten-group.jpg" alt="Group of kittens"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/pasotraspaso/4444291348/" target="_blank">Jesus Solana</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Arrays</h3>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Arrays</a> are ordered lists of values.</p>
<pre><code class="javascript">var arrayName = [value0, value1];</code></pre><br />
<p>You can put different types of data into an array.</p>
<pre><code class="javascript">var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
var lotteryNumbers = [33, 72, 64, 18, 17, 85];
var myFavoriteThings = ['Broccoli', 1024, 'Sherlock'];</code></pre>
</section>
<section>
<h3>Array Length</h3>
<p>The <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length">length</a></code> property tells you how many things are in an array.</p>
<pre><code class="javascript">var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
console.log(rainbowColors.length);</code></pre>
</section>
<section>
<h3>Using Arrays</h3>
<p>You can access items with <strong>bracket notation</strong> by using the position of the item you want.</p>
<pre><code class="javascript">var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
var firstColor = rainbowColors[0];
var lastColor = rainbowColors[6];</code></pre><br>
<p>JS arrays are <strong>zero-indexed</strong>, so counting starts at 0.</p>
</section>
<section>
<h3>Changing arrays</h3>
<p>You can use bracket notation to change an item in an array</p>
<pre><code class="javascript">var myFavoriteThings = ['Broccoli', 1024, 'Sherlock'];
myFavoriteThings[0] = 'Asparagus';</code></pre>
</section>
<section>
<h3>Expanding arrays</h3>
<p>Arrays do not have a fixed length. You can use <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push">push</a></code> to add something to an array.</p>
<pre><code class="javascript">var myFavoriteThings = ['Broccoli', 1024, 'Sherlock'];
myFavoriteThings.push('Dancing');</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Create an array of your favorite foods. <br />Print a few values onto your screen.</p>
</section>
<!-- Arrays and loops-->
<section>
<h3>Arrays + loops = BFF</h3>
<img src="dist/img/holding-hands.jpg" alt="Couple holding hands"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/mtsofan/8221708017/" target="_blank">John</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Iterating through arrays</h3>
<p>Use a <code>for</code> loop to easily work with each item in an array.</p>
<pre><code class="javascript">var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
for (var i = 0; i < rainbowColors.length; i++) {
console.log(rainbowColors[i]);
}</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Use a <code>for</code> loop to print a list of all your favorite foods.</p>
</section>
<!-- Objects -->
<section>
<h3>Objects</h3>
<img src="dist/img/matches.jpg" alt="Collection of matches"/>
<p class="credit">Photo credit: <a href="https://flic.kr/p/8RnSsa">Jirí Volejník</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>Objects</h3>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects">Objects</a> let us store a collection of properties.</p>
<pre><code class="javascript">var objectName = {
propertyName: propertyValue,
propertyName: propertyValue
};
</code></pre>
<pre><code class="javascript">var user = {
hometown: 'Atlanta, GA',
hair: 'Auburn',
likes: ['knitting', 'code'],
birthday: {month: 10, day: 17}
};</code></pre>
</section>
<section>
<h3>Accessing Objects</h3>
<p>You can retrieve values using <strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#Dot_notation">dot notation</a></strong></p>
<pre><code class="javascript">var user = {
hometown: 'Atlanta, GA',
hair: 'Auburn'
};
var usersHometown = user.hometown;</code></pre><br>
<p>Or using <strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#Bracket_notation">bracket notation</a></strong> (like arrays)</p>
<pre><code class="javascript">var usersHair = user['hair'];</code></pre>
</section>
<section>
<h3>Changing Objects</h3>
<p>You can use dot or bracket notation to change properties</p>
<pre><code class="javascript">var user = {
hometown: 'Atlanta, GA',
hair: 'Auburn'
};
user.hair = 'blue';</code></pre><br>
<p>Add new properties</p>
<pre><code class="javascript">user.married = true;
</code></pre><br>
<p>Or delete them</p>
<pre><code class="javascript">delete user.married;</code></pre>
</section>
<section>
<h3>Arrays of Objects</h3>
<p>Because arrays can hold any data type, they can also hold objects.</p>
<pre><code class="javascript">var users = [
{name: 'Jolene', age: 21},
{name: 'Alexa', age: 18}
];
for (var i = 0; i < users.length; i++) {
var user = users[i];
console.log(user.name + ' is ' + user.age + ' years old.');
}</code></pre>
</section>
<section>
<h3>Objects</h3>
<p>Just like other data types, objects can be passed into functions:</p>
<pre><code class="javascript">var jolene = {
age: 21,
hairColor: 'Auburn',
likes: ['pizza', 'tacos'],
birthday: {month: 3, day: 14, year: 1995}
}
function describeUser(user) {
console.log('You are ' + user.age + ' years old with '
+ user.hairColor + ' hair.');
}
describeUser(jolene);</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Create an object to hold information on your favorite recipe. It should have properties for:</p>
<ul>
<li><code>recipeTitle</code> (a string)</li>
<li><code>servings</code> (a number)</li>
<li><code>ingredients</code> (an array of strings)</li>
<li><code>directions</code> (a string)</li>
</ul>
<br><br>
<p>Try displaying some information about your recipe.</p>
<p>Bonus: Create a loop to list all the ingredients.</p>
</section>
<!-- Object Methods -->
<section>
<h3>Object methods</h3>
<p>Objects can also hold functions.</p>
<pre><code class="javascript">var jolene = {
age: 21,
hairColor: 'Auburn',
talk: function() {
console.log('Hello!');
},
eat: function(food) {
console.log('Yum, I love ' + food);
}
};
</code></pre><br>
<p>Call object methods using dot notation:</p>
<pre><code class="javascript">jolene.talk();
jolene.eat('pizza');</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Go back to your recipe object. Add a function called <code>letsCook</code> that says "I'm hungry! Let's cook..." with the name of your recipe title.</p>
<p>Call your new method.</p>
</section>
<!-- Final slides-->
<section>
<h3>You did it!</h3>
<img src="dist/img/fireworks.jpg" alt="Fireworks"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/markwdw85/9425240584/" target="_blank">Mark W.</a> <a href="http://creativecommons.org/licenses/by-nc/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>