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