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