|
33 | 33 |
|
34 | 34 | // Array.prototype.filter()
|
35 | 35 | // 1. Filter the list of inventors for those who were born in the 1500's
|
| 36 | + const fifteens = inventors.filter(inventor => inventor.year >= 1500 && inventor.year < 1600); |
| 37 | + console.log('Born in 1500s'); |
| 38 | + console.table(fifteens); |
36 | 39 |
|
37 | 40 | // Array.prototype.map()
|
38 | 41 | // 2. Give us an array of the inventors' first and last names
|
| 42 | + const names = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 43 | + console.log(names); |
39 | 44 |
|
40 | 45 | // Array.prototype.sort()
|
41 | 46 | // 3. Sort the inventors by birthdate, oldest to youngest
|
| 47 | + const sorts = inventors.sort((a,b) => a.year - b.year); |
| 48 | + console.log('Oldest to youngest'); |
| 49 | + console.table(sorts); |
42 | 50 |
|
43 | 51 | // Array.prototype.reduce()
|
44 | 52 | // 4. How many years did all the inventors live?
|
| 53 | + const years = inventors.reduce((agg, inv) => { |
| 54 | + return agg + (inv.passed - inv.year); |
| 55 | + }, 0) |
| 56 | + console.log(`Years lived all together: ${years}`); |
45 | 57 |
|
46 | 58 | // 5. Sort the inventors by years lived
|
| 59 | + const ages = inventors.sort((a,b) => { |
| 60 | + return (a.passed - a.year) - (b.passed - b.year); |
| 61 | + }); |
| 62 | + console.log('Sorted by number of years lived'); |
| 63 | + console.table(ages); |
47 | 64 |
|
48 | 65 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
49 | 66 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
50 |
| - |
| 67 | + const links = document.querySelectorAll('.mw-category a'); |
| 68 | + Array.from(links) |
| 69 | + .map(link => link.text) |
| 70 | + .filter(text => text.includes('de')); |
51 | 71 |
|
52 | 72 | // 7. sort Exercise
|
53 | 73 | // Sort the people alphabetically by last name
|
| 74 | + const sortedLast = people.sort((a,b) => { |
| 75 | + return a.split(', ')[0] < b.split(', ')[0] ? -1 : 1 |
| 76 | + }) |
| 77 | + console.log(sortedLast); |
54 | 78 |
|
55 | 79 | // 8. Reduce Exercise
|
56 | 80 | // Sum up the instances of each of these
|
57 | 81 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
58 |
| - |
| 82 | + const sums = data.reduce((obj, trans) => { |
| 83 | + if (!obj[trans]) { |
| 84 | + obj[trans] = 0; |
| 85 | + } |
| 86 | + obj[trans]++; |
| 87 | + return obj; |
| 88 | + }, {}); |
| 89 | + console.log(sums); |
59 | 90 | </script>
|
60 | 91 | </body>
|
61 | 92 | </html>
|
0 commit comments