|
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 inventorsBorn1500 = inventors.filter(person => person.year >= 1500 && person.year < 1599) |
| 37 | + // console.table(inventorsBorn1500) |
36 | 38 |
|
37 | 39 | // Array.prototype.map() |
38 | 40 | // 2. Give us an array of the inventors' first and last names |
| 41 | + const fullNames = inventors.map(inventor => `${ inventor.first } ${ inventor.last }`) |
| 42 | + // console.log(fullNames) |
39 | 43 |
|
40 | 44 | // Array.prototype.sort() |
41 | 45 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 46 | + const birthdaySort = inventors.sort((a, b) => a.year > b.year ? 1 : -1) |
| 47 | + // console.table(birthdaySort) |
42 | 48 |
|
43 | 49 | // Array.prototype.reduce() |
44 | 50 | // 4. How many years did all the inventors live? |
| 51 | + const totalYears = inventors.reduce((total, {year, passed}) => {return total + (passed - year)}, 0) |
| 52 | + // console.log(totalYears) |
45 | 53 |
|
46 | 54 | // 5. Sort the inventors by years lived |
| 55 | + const sortByYearsLived = inventors.sort((a, b) => (a.passed - a.year) > (b.passed - b.year) ? -1 : 1) |
| 56 | + // console.table(sortByYearsLived) |
47 | 57 |
|
48 | 58 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
49 | 59 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
50 | 60 |
|
51 | 61 |
|
52 | 62 | // 7. sort Exercise |
53 | 63 | // Sort the people alphabetically by last name |
| 64 | + const alphaByLastName = people.sort((a, b) => { |
| 65 | + const [aLast, ] = a.split(', '), |
| 66 | + [bLast, ] = b.split(', ') |
| 67 | + return aLast > bLast ? 1 : -1 |
| 68 | + }) |
| 69 | + // console.log(alphaByLastName) |
54 | 70 |
|
55 | 71 | // 8. Reduce Exercise |
56 | 72 | // Sum up the instances of each of these |
57 | 73 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
58 | | - |
| 74 | + const transportation = data.reduce((options, transport) => { |
| 75 | + if(!options[transport]) options[transport] = 0 |
| 76 | + options[transport]++ |
| 77 | + return options |
| 78 | + }, {}) |
| 79 | + // console.log(transportation) |
59 | 80 | </script> |
60 | 81 | </body> |
61 | 82 | </html> |
0 commit comments