-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6-lecture.html
332 lines (267 loc) · 7.38 KB
/
es6-lecture.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
"use strict";
// ============== MINI EXERCISE 1
// TODO: Refactor the following older JS code to use more modern syntax. The output should stay the same.
// consider how variables are created and the values are concatenated in the string
// const greatDessert = 'banana pudding';
// const badDessert = 'rhubarb pie';
// const dessertMessage = `Well, I love ${greatDessert} but I don't care for ${badDessert}.`;
// console.log(dessertMessage);
// ============== Exponentiation
// let num1 = Math.pow(4, 2); // before ES6
// let num2 = 4 ** 2; // ES6 onwards
// console.log(num1);
// console.log(num2);
// ============== for ..of loop (works on node lists!!!)
//
// const numbers = ["one", "two", "three"];
//
// for (let number of numbers) {
// console.log(number);
// }
//
// const hello = 'hello';
//
// for (let letter of hello) {
// console.log(letter);
// }
//
// const people = [
// {
// firstN: 'bob',
// lastN: 'smith'
// },
// {
// firstN: 'sally',
// lastN: 'smith'
// }
// ];
//
// // regular for loop
// for (var i = 0; i < people.length; i += 1) {
// console.log(people[i].firstN);
// }
//
// // forEach loop version
// people.forEach(function(person) {
// console.log(person.firstN);
// });
//
// // for...of loop
// for (let person of people) {
// console.log(person.firstN);
// }
// Works on strings
// const hello = 'hello';
// for (let character of hello) {
// console.log(character);
// }
// ============== MINI EXERCISE 2
// TODO: Refactor the following code to use ES6 syntax. The output should stay the same.
// consider the type of loop, concatenation and how variables are declared
// let message = '';
// let names = ['John', 'Paul', 'George', 'Ringo'];
// for (let name of names) {
// message += `Hello, ${name}\n`;
// }
// console.log(message);
// ============== MINI EXERCISE 3
// TODO: Refactor the following code to use ES6 syntax. The output should stay the same.
// consider how the function is defined
// const doubleInput = (input) => {
// return input * 2;
// }
//
// console.log(doubleInput(5));
// ============== Default Parameter Values
// Common ES5 approach
// let addArgsA = (num1, num2) => {
// if (num1 === undefined) {
// num1 = 2;
// }
// if (num2 === undefined) {
// num2 = 2;
// }
// return num1 + num2;
// };
// Better ES6 approach
// let addArgsB = (num1 = 2, num2 = 2) => num1 + num2;
// Test output
// console.log("\nA output...");
// console.log(addArgsA());
// console.log(addArgsA(1));
// console.log(addArgsA(3, 3));
//
// console.log("\nB output...");
// console.log(addArgsB());
// console.log(addArgsB(1));
// console.log(addArgsB(3, 3));
// ============== Object Assignment Shorthand
// variables to build object from...
// let breed = "Pug";
// let age = 3;
// let name = "Lexie";
// let isCute = true;
// let coat = "brindle"
// ES5 way...
// var dog = {
// breed: breed,
// age: age,
// name: name,
// isCute: isCute
// };
// //
// console.log(dog.name);
// can also assign properties using dot notation assignment.
// var dog = {};
// dog.breed = breed;
// ES6 way...
// const dog = {
// breed,
// age,
// name,
// isCute,
// coat
// };
// //
// // console.log(dog);
// //
// coat = "white";
// //
// console.log(coat)
// console.log(dog.coat)
// ============== Object / Array Destructuring
// object to destructure...
// const puppy = {
// breed: "Lab",
// age: 10,
// name: "Sabrina",
// isCute: true
// };
// let breed = puppy.breed;
// let age = puppy.age;
// let name = puppy.name;
// let isCute = puppy.isCute;
// let { breed, age, name, isCute } = puppy;
// change the values of the property variable, not the object instance
// breed = "dalmatian";
// age = 6;
// name = "Lola"
//
// console.log(breed);
// console.log(age);
// console.log(name);
// console.log(isCute);
// console.log(puppy)
// const data = {
// hum: ["32%", "35%"],
// temp: {
// degreesMetric: 34,
// degreesImperial: 56
// },
// pressure: "1100ml",
// extra: "asdfasd",
// extra1: "sdfsdf"
// }
// destructuring with arrays...
// let cats = ["CJ", "Claude", "Max"];
// let [cat1, cat2, cat3] = cats;
// let cat1 = cats[0];
// let cat2 = cats[1];
// let cat3 = cats[2];
//
//
// console.log(cat1);
// console.log(cat2);
// console.log(cat3);
// ============== Destructuring Assignment w/ Functions
// destructuring...
// const getArea = ({height, width}) => height * width;
//
// let shape = {
// height: 20,
// width: 10
// };
//
// let rectangle1 = {
// height: 40,
// width: 10
// }
//
// let rectangle2 = {
// height: 20,
// width: 40
// }
// //
// console.log(getArea(shape));
// console.log(getArea(rectangle1));
// console.log(getArea(rectangle2));
// assignment...
// let height = 20;
// let width = 10;
//
//
// console.log(getArea({height, width}));
// function buildWeatherPanel({temp: {degreesMetric, degreesImperial}, pressure, hum}) {
// // let { hum, temp, pressure } = data;
// // let {degreesMetric, degreesImperial} = temp;
// return `
// <div class="card">
// <p>${hum[1]}</p>
// <p>Metric: ${degreesMetric}, Imperial: ${degreesImperial}</p>
// <p>${pressure}</p>
// </div>
// `;
// }
//
// console.log(buildWeatherPanel(data));
// ============== MINI EXERCISE 4
// TODO: Refactor the following code to use ES6 syntax. The output should stay the same.
// consider how property values are added to the object and then used in the function
// const email = prompt('Enter an email address');
// const username = prompt('Enter a username');
// const password = prompt('Enter a password');
// const user = {
// email: email,
// username: username,
// password: password
// };
//
// function printAuthDetails(user) {
// // console.log(`The username is ${user.username}`);
// // console.log(`The email is ${user.email}`);
// console.log(`The username is ${username}`);
// console.log(`The email is ${email}`);
// }
//
// printAuthDetails(user);
// const email = prompt('Enter an email address');
// const username = prompt('Enter a username');
// const password = prompt('Enter a password');
// const user = {
// email,
// username,
// password
// };
//
// function printAuthDetails({username,email}) {
// // console.log(`The username is ${user.username}`);
// // console.log(`The email is ${user.email}`);
// console.log(`The username is ${username}`);
// console.log(`The email is ${email}`);
// }
//
// printAuthDetails(user);
</script>
</body>
</html>