|
5 | 5 | <title>Array Cardio 💪</title> |
6 | 6 | </head> |
7 | 7 | <body> |
8 | | - <p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
9 | 8 | <script> |
10 | 9 | // Get your shorts on - this is an array workout! |
11 | 10 | // ## Array Cardio Day 1 |
|
33 | 32 |
|
34 | 33 | // Array.prototype.filter() |
35 | 34 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 35 | + const born1500 = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); |
| 36 | + console.log(born1500); |
36 | 37 |
|
37 | 38 | // Array.prototype.map() |
38 | 39 | // 2. Give us an array of the inventors' first and last names |
| 40 | + const firstLast = inventors.map(inventor => (`${inventor.first} ${inventor.last}`); |
| 41 | + console.log(firstLast); |
39 | 42 |
|
40 | 43 | // Array.prototype.sort() |
41 | 44 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 45 | + const sorted = inventors.sort((a, b) => (a.year > b.year ? 1 : -1); |
| 46 | + console.log(sorted); |
42 | 47 |
|
43 | 48 | // Array.prototype.reduce() |
44 | 49 | // 4. How many years did all the inventors live? |
| 50 | + const totalYear = inventors.reduce((prev, next)=> { |
| 51 | + return prev + (next.passed - next.year) |
| 52 | + },0); |
| 53 | + console.log(totalYear); |
45 | 54 |
|
46 | 55 | // 5. Sort the inventors by years lived |
| 56 | + const yearLived = inventors.sort((a, b) => { |
| 57 | + const aLived = a.passed - a.year; |
| 58 | + const bLived = b.passed - b.year; |
| 59 | + return aLived > bLived ? -1 : 1; |
| 60 | + }); |
47 | 61 |
|
| 62 | + console.log(yearLived); |
48 | 63 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
49 | 64 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
50 | | - |
51 | | - |
| 65 | + // const category = document.querySelector('.mw-category'); |
| 66 | + // const link = Array.from(category.querySelectorAll('a')); |
| 67 | + // const de = link |
| 68 | + // .map(data => (data.textContent)) |
| 69 | + // .filter(name => name.includes('de')) |
| 70 | + // console.log(de); |
52 | 71 | // 7. sort Exercise |
53 | 72 | // Sort the people alphabetically by last name |
| 73 | + const sortLastName = people.sort((a, b) => { |
| 74 | + const [aFirst, aLast] = a.split(', '); |
| 75 | + const [bFirst, bLast] = b.split(', '); |
| 76 | + return aLast.localeCompare(bLasts); |
| 77 | + }) |
54 | 78 |
|
| 79 | + console.log(sortLastName); |
55 | 80 | // 8. Reduce Exercise |
56 | 81 | // Sum up the instances of each of these |
57 | 82 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
58 | | - |
| 83 | + const sumInstance = data.reduce((prev, next)=> { |
| 84 | + if (!prev[next]) { |
| 85 | + prev[next] = 0; |
| 86 | + } |
| 87 | + prev[next]++; |
| 88 | + return prev; |
| 89 | + },{}) |
| 90 | + console.log(sumInstance); |
59 | 91 | </script> |
60 | 92 | </body> |
61 | 93 | </html> |
0 commit comments