|
8 | 8 |
|
9 | 9 | <body> |
10 | 10 | <script> |
| 11 | + 'use strict'; |
11 | 12 | // Get your shorts on - this is an array workout! |
12 | 13 | // ## Array Cardio Day 1 |
13 | 14 |
|
|
29 | 30 |
|
30 | 31 | // Array.prototype.filter() |
31 | 32 | // 1. Filter the list of inventors for those who were born in the 1500's |
32 | | - const inventors_from_1500s = inventors.filter( |
33 | | - (inventor) => inventor.year >= 1500 && inventor.year < 1600 |
34 | | - ) |
| 33 | + const inventors_from_1500s = inventors.filter(({year}) => year >= 1500 && year < 1600); |
35 | 34 |
|
36 | | - console.table(inventors_from_1500s) |
| 35 | + console.table(inventors_from_1500s); |
37 | 36 |
|
38 | 37 | // Array.prototype.map() |
39 | 38 | // 2. Give us an array of the inventors first and last names |
40 | | - const inventors_full_name = inventors.map((inventor) => `${inventor.first} ${inventor.last}`) |
41 | | - console.table(inventors_full_name) |
| 39 | + const inventors_full_name = inventors.map(({first, last}) => first + ' ' + last); |
| 40 | + console.log(inventors_full_name); |
42 | 41 |
|
43 | 42 | // Array.prototype.sort() |
44 | 43 | // 3. Sort the inventors by birthdate, oldest to youngest |
45 | | - const inventors_by_date = inventors.sort((a, b) => a.year - b.year) |
46 | | - console.table(inventors_by_date) |
| 44 | + const inventors_by_date = inventors.sort(({year: a}, {year: b}) => a - b); |
| 45 | + console.table(inventors_by_date); |
47 | 46 |
|
48 | 47 | // Array.prototype.reduce() |
49 | 48 | // 4. How many years did all the inventors live? |
50 | 49 | const total_years_lived = inventors.reduce( |
51 | | - (currValue, inventor) => currValue += (inventor.passed - inventor.year), 0 |
52 | | - ) |
| 50 | + (currValue, {passed, year}) => currValue += (passed - year), 0 |
| 51 | + ); |
53 | 52 | console.log(total_years_lived); |
54 | 53 |
|
55 | 54 | // 5. Sort the inventors by years lived |
56 | 55 | const inventors_by_lifespan = inventors.sort( |
57 | | - (a, b) => (a.passed - a.year) - (b.passed - b.year) |
| 56 | + ({passed: aP, year: aY}, {passed: bP, year: bY}) => (aP - aY) - (bP - bY) |
58 | 57 | ); |
59 | 58 | console.table(inventors_by_lifespan); |
60 | 59 |
|
61 | 60 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
62 | 61 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
63 | 62 | /* |
64 | | - This code will be run directly from the browser console |
65 | | - after navigating to the Wikipedia link above. |
66 | | - |
| 63 | + // This code will be run directly from the browser console |
| 64 | + // after navigating to the Wikipedia link above. |
| 65 | +
|
67 | 66 | const blvd_with_de = Array.from(document.querySelectorAll('.mw-category a')) |
68 | | - .map(link => link.textContent) |
| 67 | + .map(({textContent}) => textContent) |
69 | 68 | .filter((blvd) => blvd.includes('de')) |
70 | | - */ |
71 | 69 |
|
| 70 | + */ |
72 | 71 | // 7. sort Exercise |
73 | 72 | // Sort the people alphabetically by last name |
74 | | - |
75 | 73 | const people_by_last_name = people.sort((a, b) => a.split(', ')[0] < b.split(', ')[0] ? -1 : 1); |
76 | | - |
77 | 74 | console.log(people_by_last_name); |
| 75 | + |
78 | 76 | // 8. Reduce Exercise |
79 | 77 | // Sum up the instances of each of these |
80 | | - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck']; |
| 78 | + const data = [ |
| 79 | + 'car', |
| 80 | + 'car', |
| 81 | + 'truck', |
| 82 | + 'truck', |
| 83 | + 'bike', |
| 84 | + 'walk', |
| 85 | + 'car', |
| 86 | + 'van', |
| 87 | + 'bike', |
| 88 | + 'walk', |
| 89 | + 'car', |
| 90 | + 'van', |
| 91 | + 'car', |
| 92 | + 'truck']; |
81 | 93 |
|
82 | 94 | const data_instance_count = data.reduce((tallyObject, item) => { |
83 | | - if (!tallyObject[item]) { |
84 | | - tallyObject[item] = 0; |
85 | | - } |
86 | | - tallyObject[item]++; |
87 | | - return tallyObject; |
| 95 | + tallyObject[item] ? tallyObject[item]++ : tallyObject[item] = 1; |
| 96 | + |
| 97 | + return tallyObject; |
88 | 98 | }, {}); |
| 99 | + console.dir(data_instance_count); |
89 | 100 |
|
90 | | - console.log(data_instance_count); |
91 | 101 | </script> |
92 | 102 | </body> |
93 | 103 |
|
|
0 commit comments