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