|
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 born1500 = inventors.filter(inventor => |
| 31 | + inventor.year >= 1500 && inventor.year < 1600 |
| 32 | + ); |
| 33 | + console.table(born1500); |
30 | 34 |
|
31 | 35 | // Array.prototype.map()
|
32 | 36 | // 2. Give us an array of the inventory first and last names
|
| 37 | + const names = inventors.map(inventor => |
| 38 | + `${inventor.first} ${inventor.last}` |
| 39 | + ); |
| 40 | + console.log(names); |
33 | 41 |
|
34 | 42 | // Array.prototype.sort()
|
35 | 43 | // 3. Sort the inventors by birthdate, oldest to youngest
|
| 44 | + const sortByBirthday = inventors.sort((a, b) => a.year - b.year); |
| 45 | + console.table(sortByBirthday); |
36 | 46 |
|
37 | 47 | // Array.prototype.reduce()
|
38 | 48 | // 4. How many years did all the inventors live?
|
| 49 | + const inventorsLive = inventors.reduce((count, inventor) => |
| 50 | + count + inventor.passed - inventor.year |
| 51 | + , 0); |
| 52 | + console.log(inventorsLive); |
39 | 53 |
|
40 | 54 | // 5. Sort the inventors by years lived
|
| 55 | + const sortByYearsLived = inventors.sort((a, b) => |
| 56 | + (b.passed - b.year) - (a.passed - a.year) |
| 57 | + ); |
| 58 | + console.table(sortByYearsLived); |
41 | 59 |
|
42 | 60 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
43 | 61 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
44 |
| - |
| 62 | + const category = document.querySelector('.mw-category'); |
| 63 | + // const links = [...category.querySelectorAll('a')]; |
| 64 | + const links = Array.from(category.querySelectorAll('a')); |
| 65 | + const de = links |
| 66 | + .map(link => link.textContent) |
| 67 | + .filter(name => name.includes('de')); |
45 | 68 |
|
46 | 69 | // 7. sort Exercise
|
47 | 70 | // Sort the people alphabetically by last name
|
| 71 | + const sortedByLastName = people.sort((a, b) => |
| 72 | + a.split(', ')[0] > b.split(', ')[0] ? 1 : -1 |
| 73 | + ); |
| 74 | + console.log(sortedByLastName); |
48 | 75 |
|
49 | 76 | // 8. Reduce Exercise
|
50 | 77 | // Sum up the instances of each of these
|
51 | 78 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
| 79 | + const sum = data.reduce((obj, t) => { |
| 80 | + obj[t] ? obj[t]++ : obj[t] = 1; |
| 81 | + |
| 82 | + return obj; |
| 83 | + }, {}); |
| 84 | + console.log(sum); |
52 | 85 |
|
53 | 86 | </script>
|
54 | 87 | </body>
|
|
0 commit comments