|
27 | 27 |
|
28 | 28 | // Array.prototype.filter() |
29 | 29 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 30 | + const fifteens = inventors.filter(inventor => inventor.year >= 1500 && inventor.year <1600); |
| 31 | + console.log(fifteens); |
30 | 32 |
|
31 | 33 | // Array.prototype.map() |
32 | 34 | // 2. Give us an array of the inventory first and last names |
| 35 | + const invFirstLast = inventors.map(inventor => Object.assign({}, {first: inventor.first, last: inventor.last})); |
| 36 | + console.log(invFirstLast); |
33 | 37 |
|
34 | 38 | // Array.prototype.sort() |
35 | 39 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 40 | + const invSortBirth = inventors.sort((a, b) => a.year - b.year); |
| 41 | + console.log(invSortBirth); |
36 | 42 |
|
37 | 43 | // Array.prototype.reduce() |
38 | 44 | // 4. How many years did all the inventors live? |
| 45 | + const totalYears = inventors.reduce((total, inventor) => { |
| 46 | + return total + (inventor.passed - inventor.year); |
| 47 | + }, 0) |
| 48 | + console.log(totalYears); |
39 | 49 |
|
40 | 50 | // 5. Sort the inventors by years lived |
| 51 | + const invSortLived = inventors.sort((a, b) => (a.passed - a.year) - (b.passed - b.year)); |
| 52 | + console.log(invSortLived); |
41 | 53 |
|
42 | 54 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
43 | 55 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 56 | + /** |
| 57 | + const div = document.querySelector('.mw-category'); |
| 58 | + const streets = Array.from(div.querySelectorAll('a')); |
| 59 | + const de = streets |
| 60 | + .map(street => street.textContent) |
| 61 | + .filter(street => street.included('de')) |
| 62 | + |
| 63 | + console.log(de); |
| 64 | + */ |
44 | 65 |
|
45 | 66 |
|
46 | 67 | // 7. sort Exercise |
47 | 68 | // Sort the people alphabetically by last name |
| 69 | + const peopSort = people.sort((a, b) => { |
| 70 | + const [prevLast, prevFirst] = a.split(', '); |
| 71 | + const [nextLast, nextFirst] = b.split(', '); |
| 72 | + |
| 73 | + return prevLast > nextLast ? 1 : -1; |
| 74 | + }); |
| 75 | + |
| 76 | + console.log(peopSort); |
48 | 77 |
|
49 | 78 | // 8. Reduce Exercise |
50 | 79 | // Sum up the instances of each of these |
51 | 80 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
52 | 81 |
|
| 82 | + const counts = data.reduce((dataCount, entry) => { |
| 83 | + if (dataCount[entry]) { |
| 84 | + dataCount[entry] ++; |
| 85 | + } else { |
| 86 | + dataCount[entry] = 1; |
| 87 | + } |
| 88 | + return dataCount; |
| 89 | + }, {}); |
| 90 | + |
| 91 | + console.log(counts); |
| 92 | + |
53 | 93 | </script> |
54 | 94 | </body> |
55 | 95 | </html> |
0 commit comments