-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbonus.js
More file actions
608 lines (524 loc) · 15.6 KB
/
bonus.js
File metadata and controls
608 lines (524 loc) · 15.6 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Create a function, findAverageDogAge, that takes in an array of pet objects with age properties and returns the average age of a dog.
// const pets = [
// {
// name: 'Sparky',
// type: 'Fish',
// age: 4
// },
// {
// name: 'Mr. Pig',
// type: 'Cat',
// age: 4
// },
// {
// name: 'Bubba',
// type: 'Dog',
// age: 5
// },
// {
// name: 'Pickles',
// type: 'Dog',
// age: 10
// }
//
// ];
// findAverageDogAge(pets) // returns 7.5
// users.forEach(function (user) {
// console.log(user.givenName); //forEach are just for looping through arrays.
// })
//function method
// const ages = [5, 10];
//
// function getAvg(ages) {
// const total = ages.reduce((acc, c) => acc + c, 0);
// return total / ages.length;
// }
//
// const average = getAvg(ages);
// console.log(average);
// look at each object and ask "is this a dog?" if so take that value and add it to a running total, also need to find the total to divide against the running total.
// function findAverageDogAge(pets){
// let total = 0;
// let dogCount = 0;
// pet.forEach((pet) => {
// if(pet.type === 'Dog') {
// total += pet.age;
// dogCount++;
//
// }
//
// });
// return total/dogCount;
// for(let i = 0; i< pets.length; i+=1){
// if (pets[i].type === 'Dog') {
// total += pets[i].age;
// dogCount++
// }
// }
// return total/ dogCount;
// }
//
// console.log(findAverageDogAge(pets));
// Create a function, returnPetsWithNoFish, that takes in a array of pet objects and returns an array of pet objects with no pets of type 'Fish'.
// const pets = [
// {
// name: 'Sparky',
// type: 'Fish',
// age: 4
// },
// {
// name: 'Mr. Pig',
// type: 'Cat',
// age: 4
// },
// {
// name: 'Bubba',
// type: 'Dog',
// age: 5
// },
// {
// name: 'Beans',
// type: 'Dog',
// age: 3
// },
// {
// name: 'Mr. Salmon',
// type: 'Fish',
// age: 1
// }
// ];
// function returnPetsWithNoFish(pets) {
// pets.forEach((pet) => {
// if (pet.type === 'fish') {
// return pets.slice()
// }
//
// });
// }
//didn't work (filter method)
// petsWithNoFish = pets.filter(function( pets ) {
// return pets.type !== 'fish';
// });
//
// console.log(petsWithNoFish);
// function returnPetsWithNoFish(pets) {
// const output = [];
// pets.forEach(pet => {
// if(pet.type !== 'Fish') {
// output.push(pet);
// }
// });
// return output;
// }
//
// console.log(returnPetsWithNoFish(pets));
// [
// {
// name: 'Mr. Pig',
// type: 'Cat',
// age: 4,
// },
// {
// name: 'Bubba',
// type: 'Dog',
// age: 5
// },
// {
// name: 'Beans',
// type: 'Dog',
// age: 3
// }
// ];
// ================================= WARM UP
//
// Create a function, returnLongestPetName, that takes in a array of pet objects and returns a string of the longest name for a pet.
// const pets = [
// {
// name: 'Sparky',
// type: 'Fish',
// age: 4
// },
// {
// name: 'Mr. Pig',
// type: 'Cat',
// age: 4
// },
// {
// name: 'Bubba',
// type: 'Dog',
// age: 5
// },
// {
// name: 'Beans',
// type: 'Dog',
// age: 3
// },
// {
// name: 'Mr. Salmon',
// type: 'Fish',
// age: 1
// }
// ];
// for loop
// const returnLongestPetName = (animals) => {
// let longestPetName = "";
// for (let i = 0; i < animals.length; i++) {
// if (animals[i].name.length > longestPetName.length) {
// longestPetName = animals[i].name;
// }
// }
// return longestPetName;
// };
// // forEach
// const returnLongestPetName = (animals) => {
// let longestPetName = "";
// animals.forEach((animal) => {
// if (animal.name.length > longestPetName.length) {
// longestPetName = animal.name;
// }
// });
// return longestPetName;
// };
//
//console.log(returnLongestPetName(pets));
// Write a function, returnLastTwoChars that returns the last two characters of an input string. Assume a string input of at least two characters.
// returnLastTwoChars(‘cat’) // returns ‘at’
// returnLastTwoChars(‘hello’) // returns ‘lo’
// returnLastTwoChars(‘hi’) // returns ‘hi’
//01/2/23
// function returnLastTwoChars(input) {
// return input.slice(-2);
// }
// console.log(returnLastTwoChars('cat'));
// console.log(returnLastTwoChars('hello'));
// console.log(returnLastTwoChars('hi'));
// function returnLastTwoChars(input1, input2) {
// if (input2 === 0) {
// return '';
// }
// return input1.slice(-input2);
// }
// console.log(returnLastTwoChars('cat', 0));
// console.log(returnLastTwoChars('cat', 1));
// console.log(returnLastTwoChars('cat', 2));
//01/03/23
// Write a function, iBeforeE that takes in a string and returns the string with any ‘ei’ characters replaced with ‘ie’.
// iBeforeE(‘ei’) // returns ‘ie’
// iBeforeE(‘height’) // returns ‘hieght’
// iBeforeE(‘heist’) // returns ‘hiest’
// iBeforeE(‘their’) // returns ‘thier’
// iBeforeE(‘theirtheir’) // returns ‘thierthier’
// function iBeforeE (input){
// let result = input.replaceAll("ei", "ie");
// return result;
// }
//
// console.log(iBeforeE('ei')) // returns ‘ie’
// console.log(iBeforeE('height')) // returns ‘hieght’
// console.log(iBeforeE('heist')) // returns ‘hiest’
// console.log(iBeforeE('their')) // returns ‘thier’
// console.log(iBeforeE('theirtheir')) // returns ‘thierthier’
// function iBeforeE(input) {
// let output = '';
// for (let i = 0; i < input.length; i += 1){
// if (input )
// }
// }
//
// const iBeforeE = string => string.split('ei').join('ie');
// function iBeforeE(string){
// let newString = string.replace(/ei/g, "ie");
// return newString;
// }
//01/04/23
// Create a function, filterList, that takes a list of non-negative integers and strings and returns a new list with the strings filtered out.
// const arr = [1,2,'a','b'];
//
// const filterList = arr.filter(
// element => typeof element === 'number'
// );
// console.log(filterList);
// filterList([1,2,‘a’,‘b’]) // returns [1,2]
// filterList([1,‘a’,‘b’,0,15]); // returns [1,0,15]
// filterList([1,2,‘aasf’,‘1’,‘123’,123]); // returns [1,2,123]
// function filterList(input){
// let array = []
// for(let i=0; i < input.length; i++){
// if(typeof input[i])
// }
// }
// 01/09/23
// Create a function, zipArrays, that takes two array inputs of the same length and returns an array with the elements of both arrays alternating in the order of first[0], second[0], first[1], second[1], etc. If both arrays are empty, return an empty array.
// function zipArrays(input1, input2) {
// let arrayOutput = [];
// if (input1.length === 0){
// return [];
// }
// for(let i=0, i<input1.length;i+=1){
// arrayOutput.push(input1[i]);
// arrayOutput.push(input2[i])[]
// }
// return arrayOutput;
// }
//
//
// console.log(zipArrays([], [])) // returns []
// console.log(zipArrays([1], [2])) // returns [1, 2]
// console.log(zipArrays(['a', 'b'], ['c', 'd']) // returns... ['a'), 'c', 'b', 'd']
// console.log(zipArrays([1,2,'a','b'], ['bob', null, 'sally', 25])) // returns...
//01/10/23
// Write a function, getUserCredentials, that takes in a list of user objects and returns a list of user objects with only the username and password properties. Assume at least an array of one user object.
//
// const users = [
// {
// firstName: 'Justin',
// lastName: 'Reich',
// dob: '1923-01-01',
// username: 'jreich',
// password: '$2y$10$UJlsa5vWq5DUKJjyO38gM.dCZfudWOFCrLWQosh0mhXKaZmRmvDse'
// },
// {
// firstName: 'Sally',
// lastName: 'Smith',
// dob: '1935-03-11',
// username: 'ssmith',
// password: '$2y$10$VaLGU5.7uQLr.eg6kSI9seOcP4JY4XktWt28I9JgblAGIDpkDXbya'
// },
// {
// firstName: 'Fred',
// lastName: 'Smith',
// dob: '1999-01-21',
// username: 'fsmith',
// password: '$2y$10$3USt6Dl8TNMkeh0KioPnfeVpynAotXvSIJ5xrzAHragPEAWMYEBNS'
// },
// ]
//
//
// function getUserCredentials(users) {
// const credentials = [];
// for(let i=0; i < users.length; i++){
// const user = users[i]
// credentials.push({ username: user.username, password: user.password});
// }
// return credentials;
// }
// console.log (getUserCredentials(users));
/*
[
{
username: 'jreich',
password: '$2y$10$UJlsa5vWq5DUKJjyO38gM.dCZfudWOFCrLWQosh0mhXKaZmRmvDse'
},
{
username: 'ssmith',
password: '$2y$10$VaLGU5.7uQLr.eg6kSI9seOcP4JY4XktWt28I9JgblAGIDpkDXbya'
},
{
username: 'fsmith',
password: '$2y$10$3USt6Dl8TNMkeh0KioPnfeVpynAotXvSIJ5xrzAHragPEAWMYEBNS'
}
]
*/
// 01/11/23
// This loop should print the numbers from 0 through 9 in the console
// for (let i = 0; i < 10; i+1) { //i++ was the original issue, corrected with i+1)
// console.log("The value of i is " + i);
// i++;
// }
//desired output : 0, 1 ,2, 3, 4, 5, 6, 7, 8, 9
// Create a function, shiftElementsOver, that takes in an array and returns the array with all elements moved one index further into the array. The final element should be moved to the front. Assume the input array will have three or more elements.
// function shiftElementsOver(arr, places) {
// let shiftedArray = [];
// for (var i = 0; i < places; i++) {
// shiftedArray.unshift(arr.pop());
// }
// }
// console.log(shiftElementsOver());
//
// shiftElementsOver([1, 2, 3, 4, 5]) // returns [5, 1, 2, 3, 4]
// shiftElementsOver(['bob', 'sally', 'mary']) // returns ['mary', 'bob', 'sally']
// shiftElementsOver([null, 123, 'hello world', true]) // returns [true, null, 123, 'hello world']
//01/18/23
// Write a function that takes a neighborhood object and determines if it is desirable. A neighborhood is desirable if the median home price is less than 300000, crime rates are low, and the total rating of schools is at least 24.
// example data...
// const neighborhood1 = {
// neighborhood: "Lovely Estates",
// medianHomePrice: 280000,
// pool: true,
// tennis: false,
// crimeRate: "low",
// schools: [
// {name: "Elementary School", rating: 8},
// {name: "Middle School", rating: 6},
// {name: "High School", rating: 8}
// ]
// };
//
// const neighborhood2 = {
// neighborhood: "Luminous Estates",
// medianHomePrice: 270000,
// pool: true,
// tennis: false,
// crimeRate: "high",
// schools: [
// {name: "Elementary School", rating: 8},
// {name: "Middle School", rating: 8},
// {name: "High School", rating: 8}
// ]
// }
// const neighborhood3 = {
// neighborhood: "Oak Mountain",
// medianHomePrice: 290000,
// pool: false,
// tennis: false,
// crimeRate: "low",
// schools: [
// {name: "Elementary School", rating: 8},
// {name: "Middle School", rating: 8},
// {name: "High School", rating: 8}
// ]
// }
//
// const neighborhood4 = {
// neighborhood: "Ginormous Acres",
// medianHomePrice: 350000,
// pool: true,
// tennis: true,
// crimeRate: "low",
// schools: [
// {name: "Elementary School", rating: 9},
// {name: "Middle School", rating: 9},
// {name: "High School", rating: 9}
// ]
// }
// function goodNeighborhood(neighborhood){
// let totalSchoolRating = 0;
//
// for (let i = 0; i < neighborhood.schools.length; i += 1) {
// totalSchoolRating += neighborhood.schools[i].rating;
// }
// return neighborhood.medianHomePrice < 300000 && neighborhood.crimeRate === 'low' && totalSchoolRating >= 24;
// }
//
// console.log(goodNeighborhood(neighborhood1)) // returns false due to school rating
// console.log(goodNeighborhood(neighborhood2)) // returns false due to crime rate
// console.log(goodNeighborhood(neighborhood3)) // returns true
// console.log(goodNeighborhood(neighborhood4)) // returns false due to median home price
01/19/2023
// Create a function, encodeStr, that takes in a string and returns the string of characters with the following substitutions:
//
// function encodeStr (input) {
// let inputNew = input.toLowerCase()
// if (typeof input === 'string') {
// inputNew.replace('a', '@');
// }
// if (typeof input === 'string') {
// inputNew.replace('i', 'i');
// }
// if (typeof input === 'string') {
// inputNew.replace('s', '$');
// }
// }
// function encodeStr (input) {
// return input
// .replace(/a/g, "@")
// .replace(/S/g, "$")
// .replace(/i/g, "1");
// }
//replaceALL()
// if(typeof input === 'i' || input === 'I'){
// return input.replace('1');
// }
// if(typeof input === 's' || input === 'S'){
// return input.replace('$')
// }
// }
// ‘a’ or ‘A’ becomes ‘@’
// ‘i’ or ‘I’ becomes ‘1’
// ‘s’ or ‘S’ becomes ‘$’
//
// console.log(encodeStr("apple")) // returns ‘@pple’
// console.log(encodeStr("codeup")) // returns ‘codeup’
// console.log(encodeStr("SASS")) // returns ‘$@$$’
// console.log(encodeStr("bike")) // returns ‘b1ke’
// 01/24/23
// Make a function, reverseStrings, that takes in an array of objects and reverses the value of the 'str' properties.
const strs = [
{
id: 1,
str: 'hello'
},
{
id: 2,
str: 'world'
},
{
id: 3,
str: 'codeup'
},
{
id: 4,
str: 'x'
}
]
// function reverseStrings(strs){
// for(let i = 0; i < str.length; i += 1) {
// str[i].str = strs[i].str.split('').reverse().join('');
// }
// return strs;
// }
//
// reverseStrings(strs) // returns...
// function reverseStrings(strs){
// strs.forEach(function(strObj){
// strs.obj.str = strObj.str.split('').reverse().join('');
// });
// return strs;
// }
// console.log(reverseStrings(strs));
// findAverageDogAge(pets) // returns 7.5
// users.forEach(function (user) {
// console.log(user.givenName); //forEach are just for looping through arrays.
// })
//function method
// const ages = [5, 10];
//
// function getAvg(ages) {
// const total = ages.reduce((acc, c) => acc + c, 0);
// return total / ages.length;
// }
//
// const average = getAvg(ages);
// console.log(average);
//
// [
// {
// id: 1,
// str: 'olleh'
// },
// {
// id: 2,
// str: 'dlrow'
// },
// {
// id: 3,
// str: 'puedoc'
// },
// {
// id: 4,
// str: 'x'
// }
// ]
//01/26/23
// map method
// let names = users.map(user) => {
// return user.name;
// }
function logFood (arr) {
for(let i = 0; i < arr.length; i++) {
console.log(`My fav food ${arr[i]}`)
}
}
let foodArray = ['pizza', 'cheese', 'frito pie', 'BBQ'];
let newSortedArray = foodArray.sort()
logFood(newSortedArray)