forked from DevMountain/javascript-2-afternoon-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraysPractice.js
346 lines (221 loc) · 8.96 KB
/
arraysPractice.js
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
//Once you complete a problem, open up Chrome and check the answer in the console.
var arr = [10,20,30];
//Create a function named 'first' that is given 'arr' as the argument and returns the first item in the given array.
//Code Here
var first = (arr) => arr[0];
//Next problem
var arr = [40,50,60];
//Create a function named 'last' that is given 'arr' as the argument and returns the last item in the given array.
//Code Here
var last = (arr) => arr[arr.length - 1];
//Next Problem
var family = ['Tyler', 'Jordyn', 'Ryan', 'Chelsey', 'Ireland'];
//Create a function named 'looper' that is given family as it's only argument, loops through the given array, and alerts every item in the array.
//Code Here
var looper = (family) => {
for(member in family){
alert(family[member]);
}
}
//Next problem
var letters = ['A', 'B', 'C', 'D', 'E'];
//Write a function called reversedLooper that is given letters as it's only argument and loops through the given array backwards alerting every item in the array starting at the end.
//Code Here
var reversedLooper = letters => {
for(let i = letters.length-1; i >=0; i--) {
alert(letters[i]);
}
}
//Next Problem
var nums = [1,2,3,6,22,98,45,23,22,12];
//Write a function named evenFinder that is given nums as it's only argument and removes all values that aren't even from the given array.
//Code Here
var evenFinder = nums => nums = nums.filter( (num)=>num%2==0 );
console.log(nums);
// =============================================
// =============================================
// EXTRA PRACTICE PROBLEMS BELOW
// =============================================
// =============================================
//Next problem
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
//Write a function called divider that is given one argument, numbersArray.
//Have divider return an Array with the first item in the array being the evens array (all the even values from numbersArray) and the second item in the Array being the odds array (all the odd values from numbersArray).
//Code Here
var divider = numbersArray => [numbersArray.filter( (num)=>num%2==0), numbersArray.filter( (num)=>num%2!=0)];
//Next Problem
var getRandomArbitrary = function() {
return Math.floor(Math.random() * 30);
};
// var numbers = [0,3,4,5,6,7,9,14,17,24,25,26,29,30];
//Above you're given a function that will return a random number between 0 and 30. There is also a commented out array full of numbers to help you visualize what your function will be receiving.
// Your job is to write a function named finder that will get a random number (by invoking getRandomArbitrary), then loop through the array (that will be passed in as a parameter) to see if that random number is in the array. If it is, return true, if it's not, return false
//Code Here
var finder = arr => {
var randall = getRandomArbitrary();
for(let i = 0; i < arr.length; i++) {
if(arr[i] === randall){
return true;
}
}
return false;
}
//Next Problem
var myGroceryList = ['chips', 'pizza', 'hotpockets', 'MtnDew', 'corndogs'];
/*
Here we're going to write a function that mimics going shopping and checking things off of our grocery list,
and adding new items to our list.
Write a function called removeItem that is given two arguments, the first is myGroceryList, and the
second is an item to remove from myGroceryList. If the second argument (or the item to add or remove) matches an item in myGroceryList,
remove that item from the your grocery list and return the new, updated grocery list.
Once you do that, write another function called addItem that is given two arguments, the first is myGroceryList
and the second is an item to add to your grocery list. In addItem add the item you passed in to
myGroceryList then return the new, updated grocery list.
In both the removeItem function and the addItem function, you will also need to check for valid aurguments. Specrunner will try to call your functions without passing in valid aurguments. When this happens, you will need to respond by returning an empty array.
*/
//Code Here
var removeItem = (myGroceryList, item) => {
for(let i = myGroceryList.length - 1; i >= 0; i--){
if(myGroceryList[i] === item){
myGroceryList.splice(i,1);
}
}
return myGroceryList;
}
var addItem = (myGroceryList, item) => {
myGroceryList.push(item);
return myGroceryList;
}
//removeItem(myGroceryList, 'chips') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs'];
//addItem(myGroceryList, 'Jerky') --> ['pizza', 'hotpockets', 'MtnDew', 'corndogs', 'Jerky'];
//Next Problem
//Write a function called maker that creates an array, fills that array with numbers from 1 to 215, then returns the array.
//Code Here
var maker = () => {
var arr = [];
for(let i=0; i<215; i++){
arr[i] = i+1;
}
return arr;
}
//Next Problem
var numbers = [5, '9', 16, 19, '25', '34', 48];
//Write a function called addTen that is given 'numbers' as it's only argument and returns a new
//array after adding ten to each item in numbers. *Verify your answer is correct. --> [15, 19, 26, 29, 35, 44, 58]
//Code Here
// var addTen = numbers => {
// for(let i = 0; i < numbers.length; i++){
// numbers[i] = Number(numbers[i]) + 10;
// }
// return numbers;
// }
var addTen = numbers => numbers.map((num)=> num = Number(num) + 10);
//Next Problem
var num1 = Math.floor(Math.random() * 30);
var num2 = Math.floor(Math.random() * 30);
var arr1 = [];
var arr2 = [];
for(var i = 0; i < num1; i++){
arr1.push(i);
}
for(var i = 0; i < num2; i++){
arr2.push(i);
}
//Above is some code that adds a random number of values to both arr1 and arr2.
//Write a function called 'longer' that is given arr1 and arr2 as it's only arguments. Return the array which is longest.
//Code Here
var longer = (arr1, arr2) => arr1.length > arr2.length ? arr1 : arr2;
/*
As a continuation of the previous problem, write another function called 'both'.
Your 'both' function will be given two arguments, arr1 and arr2 (from the previous example).
'both' should return a new array with the matching numbers found in both arr1 and arr2.
Example: var arr1 = [1,2,3,4]; var arr2 = [2,4,5,6]; newArray // [2,4]
*/
//Code Here
var both = (arr1, arr2) => {
let newArr = [];
for(let i = 0; i < arr1.length; i++){
if(arr2.includes(arr1[i])){
newArr.push(arr1[i]);
}
}
return newArr;
}
//NEXT PROBLEM
var devMountainEmployees = [];
var tyler = {
name: 'Tyler',
position: 'Lead Instructor/Engineer',
spiritAnimal: 'Honey Badger'
};
var cahlan = {
name: 'Cahlan',
position: 'CEO',
spiritAnimal: 'butterfly'
};
var ryan = {
name: 'Ryan',
position: 'Marketing',
spiritAnimal: 'fox'
};
var colt = {
name: 'Colt',
position: 'Everything really',
spiritAnimal: 'Young Male Horse'
};
/*Above you're given an empty array with four objects. Fill the devMountainEmployees
array with those four objects. After that console.log the length of the Array and make
sure that it's equal to 4. */
//Code Here
devMountainEmployees.push(tyler, cahlan, ryan, colt);
console.log(devMountainEmployees.length);
/*Now let's say Cahlan has a mental breakdown and has to take a leave of absence to 'find himself'.
Loop through your devMountainEmployees until you find cahlan, then remove him from the array.*/
//Code Here
for(let i = devMountainEmployees.length - 1; i >= 0; i--){
if(devMountainEmployees[i].name === "Cahlan"){
devMountainEmployees.splice(i,1);
}
}
//NEXT PROBLEM
/*A very clean way to pass around large LISTS (arrays) of COLLECTIONS (objects)
of Data is to have an Array full of objects. */
//Create an empty array called users.
//Code Here
var users = [];
/*Now add three user objects to your users array. Each user object should contain the
following properties. name, email, password, username.*/
//include this as one of the objects in your array.
var user1 = {
name: 'Tyler McGinnis',
email: '[email protected]',
password: 'iLoveJavaScript',
username: 'infiniateLoop'
};
//Your Code Here
users.push(user1);
users.push({
name: "Billy Bob",
email: '[email protected]',
password: 'myNameIsBillyBob',
username: 'billyBaller12'
});
users.push({
name: "Rupert Johnston",
email: '[email protected]',
password: 'idk990!',
username: 'pugsAreGreat'
})
/*Now you have a very common data structure. Twitter is a good use case.
It's easy to imagine that your followers list on Twitter is an Array full or objects
and those objects contain properties about the specific person you follow.*/
/*Now let's say that Tyler decided to delete his account. Loop through your array of
objects until you find Tyler's account (use [email protected] to find him).
Once you find the particular index he's located in, delete him from the array.*/
//Code Here
for(let i = users.length - 1; i >= 0; i--){
if(users[i].name === "Tyler McGinnis"){
users.splice(i,1);
}
}
//The activity we just did is very much how data works in 'the real world'.