-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.html
340 lines (327 loc) · 12.9 KB
/
day4.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>reveal.js</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/momentum.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/atom-one-dark.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<script defer src="https://use.fontawesome.com/releases/v5.0.2/js/all.js"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Code Basics</h1>
<h2 class="brand-teal">More JavaScript</h2>
</section>
<section>
<h2>Today we'll learn about...</h2>
<ul>
<li>functions and arguments</li>
<li>arrays and objects</li>
<li>loops</li>
</ul>
</section>
<section>
<h3>Functions</h3>
<p>Functions are reusable pieces of code. They can be named, like variables, and called when you need them.</p>
<pre><code data-trim>
function chihuahuaFacts() {
console.log("Chihuahuas are the smallest of all dogs.")
console.log("Chihuahuas can be 'apple-headed' or 'deer-headed.'")
}
</code></pre>
<aside class="notes">Spend time explaining the syntax of a function. Run it in the console to show the difference between declaring and
calling a function.</aside>
</section>
<section>
<h3>Functions with Arguments</h3>
<p>Functions can accept one or more input values, called "arguments."</p>
<pre><code data-trim>
function callDog(dogName){
console.log("Come here, " + dogName + "!");
}
callDog("Charlie");
// "Come here, Charlie."
</code></pre>
<aside class="notes">
<p>Demo this function in the console. Demo writing a function that accepts more than a single argument.</p>
</aside>
</section>
<section>
<h3>CODE BREAK</h3>
<p>Write a function to calculate someone's age and output the result to the console in a string, like "You are 52 years
old!".
</p>
<p>It should accept a single argument: a year of birth.</p>
</section>
<section>
<h3>Function return values</h3>
<p>Functions can return a value, allowing them to be used for common calculations.</p>
<pre><code data-trim>
function miles2Kilometers(miles) {
return miles * 1.60934;
}
var kilometers = miles2Kilometers(3)
console.log(kilometers);
// 4.82802
</code></pre>
</section>
<section>
<h3>Multiple return values</h3>
<p>Functions can have multiple return values. The first one to occur ends the function.</p>
<pre><code data-trim>
function letterGrade(score) {
if (score >= 90) {
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70) {
return "C";
} else if (score >= 60) {
return "D";
}
return "F";
}
</code></pre>
</section>
<section>
<h3>CODE BREAK</h3>
<p>Write a function that converts temperature from Fahrenheit to Celsius.</p>
<p>To convert Fahrenheit to Celsius, deduct 32, then multiply by 5, then divide by 9.</p>
<p>Try writing this code by editing
<a href="https://glitch.com/edit/#!/momentum-f2c?path=script.js:7:0">this Glitch program</a>.</p>
</section>
<section>
<h3>Arrays</h3>
<p>Arrays are like lists for multiple values. Values are listed inside square brackets (
<span class="code-snippet">[]</span>) and separated by commas.</p>
<pre><code data-trim>
var emptyArray = [];
var arrayOfNumbers = [2, 4, 6, 8];
var arrayOfStrings = ["cats", "dogs", "hamsters"];
var arrayOfDifferentStuff = [23, null, "octopus", true];
var arrayOfArrays = [arrayOfNumbers, arrayOfStrings];
</code></pre>
<aside class="notes">Ask students to run this code themselves in their console. What would arrayOfArrays look like when eval'd?</aside>
</section>
<section>
<h3>Using Arrays</h3>
<p>Find out how many values are in an array:</p>
<pre><code data-trim>
var colors = ["red", "green", "blue"];
colors.length
</code></pre>
<p>You can also retrieve values from an array:</p>
<pre><code data-trim>
var firstColor = colors[0];
var lastColor = colors[2];
</code></pre>
<aside class="notes">
<p>Demo arrays in the console and/or on the whiteboard. Show how items can be added or changed. Explain zero-indexing.
</p>
</aside>
</section>
<section>
<h3>CODE BREAK</h3>
<p>Write a shopping list of the foods you need to make your favorite recipe as an array.</p>
<p>Can you access the second and third items in your list and output them to the console?</p>
</section>
<section>
<h3>Loops</h3>
<p>A loop is a code structure that lets us repeat a task until a certain condition is met.</p>
<p>
<span class="code-snippet">For</span> loops are commonly used to count a certain number of times (iterations) to repeat a task.</p>
<pre><code data-trim>
for ([initial value]; [condition]; [final expression]) {
// do this thing
}
</code></pre>
<pre><code data-trim>
// count 1 to 10
for (var i = 1; i <= 10; i++) {
console.log(i);
}
</code></pre>
<aside class="notes">
<p>Spend time explaining what each part does. The concept of iteration is confusing at first!</p>
<p>It is useful to explain that
<span class="code-snippet">i++</span> is equivalent to
<span class="code-snippet">i = i + 1</span>
</p>
</aside>
</section>
<section>
<h3>CODE BREAK</h3>
<p>Run the code from the preceeding slide in your console.</p>
<p>Experiment with changing parts of the expressions.</p>
<p>Try writing a for loop that will output the first twelve squares to the console.</p>
<p>(e.g., 1
<sup>2</sup> = 1 through 12
<sup>2</sup> = 144) </p>
</section>
<section>
<h3>Loops and arrays</h3>
<p>When we put loops and arrays together, we can perform operations on the entire array.</p>
<pre><code data-trim="true">
// Print each student
var students = ["Dale", "Cadence", "Alex", "Nevada"];
for (var index = 0; index < students.length; index++) {
console.log(students[index]);
}
</code></pre>
<aside class="notes">
Walk through this example step by step. Make sure to talk about why we use < instead of <=.
</aside>
</section>
<section>
<h3>CODE BREAK</h3>
<p>Write a for loop that will take an array of numbers, like so:</p>
<p>
<code>var numbers = [7, 2, -1, 18];</code>
</p>
<p>The loop should print the square of each number.</p>
<p>Try turning this into a function.</p>
</section>
<section>
<h3>Putting functions and loops together</h3>
<p>We can use functions and loops together to create new arrays or extract a piece of information from an array.</p>
<pre><code data-trim="true">
// Return the average of an array.
function average(numbers) {
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum / numbers.length;
}
</code></pre>
</section>
<section>
<h3>Objects</h3>
<p>An object in JavaScript is a more complex data structure that can store pairs of keys and values. It is a little
like a dictionary.</p>
<pre><code data-trim>
var anObject = {
key: value,
key: value
};
</code></pre>
<pre><code data-trim>
var aboutMe = {
hometown: "New York, NY",
almaMater: "UNC",
likes: ["avocadoes", "chihuahuas", "code"],
zodiacSign: "Scorpio"
};
</code></pre>
<aside class="notes">Demo examples in the console. Note comma-separated values, not semicolons.</aside>
</section>
<section>
<h3>Accessing Object Data</h3>
<p>You can retrieve values using "dot notation"</p>
<pre><code data-trim>
var aboutMe = {
hometown: "New York, NY",
almaMater: "UNC",
likes: ["avocadoes", "chihuahuas", "code"],
zodiacSign: "Scorpio"
};
var myHometown = aboutMe.hometown;
</code></pre>
<p>...or using "bracket notation" (like arrays)</p>
<pre><code data-trim>
var mySign=aboutMe['zodiacSign'];
</code></pre>
<aside class="notes">
Demo accessing object data in the console. Ask why you might use bracket notation over dot notation.
</aside>
</section>
<section>
<h3>CODE BREAK</h3>
<p>Create an object in your JS file that holds information about a book. It should have properties for:</p>
<ul>
<li>the title (a string)</li>
<li>author (a string)</li>
<li>alreadyRead (a boolean indicating if you read it yet)</li>
</ul>
<p>Add other properties if you like.</p>
</section>
<section>
<h3>Putting all of this together</h3>
<p>Functions, arrays, loops, and objects are the fundamental parts of programming. There are more things to learn, but
with these tools, you can write almost any program you want. When you combine them, you can do very powerful things.
</p>
</section>
<section>
<h3>Arrays, objects, loops, and functions</h3>
<pre><code data-trim="true">
var students = [{name: "Trinidad", age: 27},
{name: "Scout", age: 53},
{name: "Kai", age: 31}];
function getAges(people) {
var ages = [];
for (var i = 0; i < people.length; i++) {
ages.push(people[i].age);
}
return ages;
}
getAges(students); // [27, 53, 31]
</code></pre>
</section>
<section>
<h3>CODE BREAK</h3>
<p>Take the code from the previous example. Make a function called getAverageAge that takes an array of students and
returns the average age of the students.
</p>
</section>
<section>
<h3>HOMEWORK 1</h3>
<p>Create more book objects and store them in an array.</p>
<p>Write a loop that iterates through the array of books and logs each book title to the console.</p>
<p>BONUS: Write a function that creates a reading list from of books you haven't read and outputs that list to the console.</p>
<p>But wait, there's more
<i class="fas fa-angle-double-right brand-salmon"></i>
</p>
</section>
<section>
<h3>HOMEWORK 2</h3>
<p>Write a function that converts a Fahrenheit temperature to a color. You can determine the colors.</p>
<p>Call this function
<code>fahrenheitToColor</code> and add it to the Glitch project for your
<code>fahrenheitToCelsius</code> function.</p>
</section>
</div>
<footer class="footer">
<img src="images/logo-main.png" alt="">
</footer>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function () { hljs.initHighlightingOnLoad(); } }
],
history: true
});
</script>
</body>
</html>