-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray Manipulation Lecture.html
398 lines (282 loc) · 9.5 KB
/
Array Manipulation 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<title>Array Manipulation Lecture</title>
</head>
<body>
<main class="container">
<h1>Array Manipulation Lecture</h1>
</main>
<script>
"use strict";
// ============================= Adding and Removing Elements
/*
// The following array methods change the original array value!
someArray.push() = adds new last element
someArray.pop() = removes last element
someArray.unshift() = adds new first element
someArray.shift() = removes first element
*/
const pies = [
"apple",
"cherry",
"key lime",
"huckleberry",
"rhubarb"
];
// push
pies.push('Vanilla');
console.log(pies);
// pop
// console.log(pies);
// const x = pies.pop();
// console.log(x);
// pies.pop();
// console.log(pies);
// unshift
//
// pies.unshift('vanilla');
// console.log(pies);
// shift
// const removedFirstElement = pies.shift();
// console.log(removedFirstElement);
// console.log(pies);
// use these method to use array in several
// ============================= !! MINI-EXERCISE 1 !!
// // 1. Create an array the string elements 'April', 'May', 'June'
// const arrayString = ["April", "May", "June"];
//
// // 2. Add 'July' in the correct place of the array
// arrayString.push('July');
// console.log(arrayString);
//
// // 3. Add 'March' in the correct place of the array
// arrayString.unshift('March');
// console.log(arrayString);
//
//
// // 4. July is too hot; remove it from the array.
// // const removeJuly = arrayString.shift('July');
// // console.log(removeJuly;
// // console.log(arrayString);
// arrayString.pop();
// console.log(arrayString);
// Console log the result and verify you get ['March', 'April', 'May', 'June']
// ============================= Slicing
// RETURNS A SUB ARRAY COPY OF THE ORIGINAL
/* SYNTAX
someArray.slice(startingIndex, startingIndexNotIncluded);
// one argument only will return a copy from the starting index to the end of the array
*/
// const pies = [
// "apple",
// "cherry",
// "key lime",
// "huckleberry",
// "rhubarb",
// "pumpkin"
// ];
// const firstTwoPies = pies.slice(0, 2);
// console.log(pies.slice(0, 2));
// console.log(pies.slice(2));
// ** create a function that takes in an array of pies baked and return the 3 most recently baked pies
// function threeMostRecentPies(pies){
// return pies.slice(pies.length -3);
// }
//
// console.log(threeMostRecentPies(pies));
// ============================= Copying Array Values
// const x = [1, 2, 3];
// const y = x; //copies the array, they are the same, if you change x you change y also.
// const z = y;
// console.log(x)
// console.log(y)
// x.push(4);
// console.log(x);
// console.log(y);
// console.log(z);
// Copies the array values
// const x = [1, 2, 3];
// const y = x.slice();
// x.push('hello');
// console.log(x);
// console.log(y);
// ============================= Sorting
//
// const pies = [
// "apple",
// "rhubarb",
// "key lime",
// "cherry",
// "huckleberry"
// ];
// //
//const piesOriginalOrder = pies.slice(); // use slice to
// const sortedPies = pies.slice().sort(); //method chaining
//
// pies.sort(); // change the original value of the array
// console.log(pies);
// //console.log(piesOriginalOrder);
// console.log(sortedPies);
// custom sorting: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
// const numbers = [1, 11, 2, 5];
// function compareNumbers(a, b) {
// return a - b; // a-b because of number.sort JS will sort the numbers numerically
// }
// numbers.sort(compareNumbers);
//
// console.log(numbers);
// ============================= Reversing
//
// const pies = [
// "apple",
// "huckleberry",
// "cherry",
// "rhubarb",
// "key lime"
// ];
// const reverseAlpOrder = pies.slice().sort().reverse()
// console.log(pies);
// console.log(reverseAlpOrder);
// const reversedPies = pies.slice().reverse();
// // pies.reverse(); // will mutate the array and cause the array to be different from the original.
//
// console.log(pies);
// console.log(reversedPies);
// ============================= Split / Join
// ======Mutates Values
// push, pop, shift, unshift, sort, reverse
//
// =====Does not mutates
// slice
// split
// join
// splitting string into and array
//spitting only works for strings.
//split and join duplicate the original value, they don't mutate the original.
// const names = "Bob,Sally,Mary";
// // const namesArr = names.split(" "); //the split will take the input and look for what you ask
// const namesArr = names.split(",");
// // const namesArr = names.split("a"); //
// // const namesArr = names.split(""); //breaks ever letter in the string into their own element.
// // console.log(namesArr);
// // console.log(names);
// //
// const namesString = namesArr.join("");
// console.log(namesString);
// const PI = 3.14;
//
// function returnDeceimal(num){
// return Number('.' + String(num).split('.')[1]);
// }
//
// console.log(returnDeceimal(PI))
// splitting on an empty string
// const everyCharacter = bondsString.split("");
// joining array into a string
// const bondsArray = ["Connery", "Lazenby", "Moore", "Dalton", "Brosnan", "Craig"];
// const bondsString = bondsArray.join("");
// console.log(bondsString);
//
//
// let output = "";
// for (var i = 0; i < bondsArray.length; i += 1) {
// output += bondsArray[i];
// output += ", ";
// }
//
// console.log(bondsString);
// ============================= !! MINI-EXERCISE 2 !!
// 1. Put the first names of everyone in your row in the order they are sitting (just your half of the classroom)
// const rowNames = ['Kenneth', 'Robert', 'Luke'];
// 2. Log the alphabetical order of everyone in your row
//const originalRow = rowNames.slice()
// const alpRow = originalRow.sort();
// console.log(alpRow);
// console.log(rowNames);
// 3. Log the reverse alphabetical order of everyone in your row
// const reverseAlpRow = originalRow.sort().reverse()
// console.log(reverseAlpRow);
// 4. Log everyone in the row in reverse order
// const reverseRow = originalRow.reverse()
// console.log(reverseRow);
// 5. Log an array of just the first two students in the row (left to right)
// const firstTwoStudents = rowNames.slice(0, 2);
// firstTwoStudents.forEach(function(name) {
// console.log(name);
// });
// 6. Log everyone in the row in a single string separated by spaces
// const rowNames = "Kenneth,Robert,Luke";
// const rowArr = rowNames.split(",");
//
// const splitSingleStringNames = rowArr.join(""); //the split will take the input and look for what you ask
// console.log(splitSingleStringNames);
// 7. Log everyone in the row in a single string separated by underscores
// const rowNames = "Kenneth,Robert,Luke";
// const rowArr = rowNames.split(",");
//
// const splitSingleStringNames = rowArr.join("_"); //the split will take the input and look for what you ask
// console.log(splitSingleStringNames);
// ============================= (EXTRA INFO) Splicing Elements
/*
someArray.splice(param1, param2, param3...);
param1 = which index to start from
param2 = how many elements to remove
param3 = from this parameter and onward, arguments passed in will be added as new elements at the end of the array
*/
//
// // create new test array
// var bonds = ["Craig", "Brosnan", "Dalton", "Moore", "Connery"];
//
//
// // removing elements splice
// var missingBonds = bonds.splice(bonds.indexOf("Moore"), 2);
// console.log(bonds);
// console.log(missingBonds);
//
//
// // adding elements with splice
// bonds.splice(1, 0, "Lazenby");
// console.log(bonds);
//
//
// // replace elements
// bonds.splice(bonds.indexOf("Craig"), 1, "Elba");
// console.log(bonds);
// ============================= WRITING FUNCTIONS WITH ARRAYS
// var pies = [
// "apple",
// "tasty cherry",
// "tasty key lime",
// "huckleberry",
// "rhubarb"
// ];
// Create a function, getTastyPies, that takes in an array of strings and returns an array of strings that start with "tasty"
// function getTastyPies(pies) {
// var tastyPies = [];
// pies.forEach(function(pie) {
// if (pie.startsWith("tasty")) {
// tastyPies.push(pie);
// }
// });
// return tastyPies;
// }
//
// console.log(getTastyPies(pies));
// console.log(pies);
// ** Create a function that will take in a formatted string of numbers and return an array of phone numbers without any symbols. Console.log the output of the returned array.
/* EXAMPLE...
const phoneNumbers = '210-555-2020\n230-555-2020\n512-555-3030';
cleanPhoneNumbers(phoneNumbers);
the above code should output the following...
2105552020
2305552020
5125553030
*/
// const phoneNumbers = '210-555-2020\n230-555-2020\n512-555-3030';
</script>
</body>
</html>