|
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 | + console.log(`Inventors that were born in the 1500s: `, inventors.filter(inventor => inventor.year >= 1500 && inventor.year <= 1599)); |
34 | 35 |
|
35 | 36 | // Array.prototype.map()
|
36 | 37 | // 2. Give us an array of the inventors first and last names
|
37 |
| - |
| 38 | + console.log(`All the first and last names of inventors: `, inventors.map(inventor => ({first: inventor.first, last: inventor.last}))); |
| 39 | + |
38 | 40 | // Array.prototype.sort()
|
39 | 41 | // 3. Sort the inventors by birthdate, oldest to youngest
|
| 42 | + console.log(`All the inventors sorted by birthday (oldest to youngest): `, inventors.sort((a, b) => a.year < b.year ? -1 : 1)); |
40 | 43 |
|
41 | 44 | // Array.prototype.reduce()
|
42 | 45 | // 4. How many years did all the inventors live all together?
|
| 46 | + console.log(`The amount of years the inventors lived, accumulated: `, inventors.reduce((counter, item) => counter + (item.passed - item.year), 0)); |
| 47 | + |
43 | 48 |
|
44 | 49 | // 5. Sort the inventors by years lived
|
| 50 | + console.log(`The inventors sorted by how many years they lived: `, inventors.sort((a, b) => (a.passed - a.year) < (b.passed - b.year) ? 1 : -1)); |
| 51 | + |
45 | 52 |
|
46 | 53 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
47 | 54 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
48 |
| - |
| 55 | + /* |
| 56 | + const mw = document.querySelector('.mw-category'); |
| 57 | + const titles = mw.from(document.querySelector('a')).map(item => item.textContent); |
| 58 | + const de = titles.filter(title => title.includes('de')); |
| 59 | + */ |
49 | 60 |
|
50 | 61 | // 7. sort Exercise
|
51 | 62 | // Sort the people alphabetically by last name
|
52 |
| - |
| 63 | + console.log(`The people's names sorted alphabetically: `, inventors.sort((a, b) => a.last.split("")[0] > b.last.split("")[0] ? 1 : -1)); |
| 64 | + |
53 | 65 | // 8. Reduce Exercise
|
54 | 66 | // Sum up the instances of each of these
|
55 | 67 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
56 |
| - |
| 68 | + console.log(`Sum of instances of cars: `, data.reduce((counter, car) => { |
| 69 | + if(!counter[car]) { |
| 70 | + counter[car] = 1; |
| 71 | + }else { |
| 72 | + counter[car] += 1; |
| 73 | + } |
| 74 | + return counter; |
| 75 | + }, {})); |
| 76 | + |
57 | 77 | </script>
|
58 | 78 | </body>
|
59 | 79 | </html>
|
0 commit comments