|
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 | | - |
| 30 | + const renaissancers = inventors.filter(inventor => inventor.year >= 1500 && inventor.year < 1600); |
| 31 | + renaissancers.forEach(renaissancer => console.log(`${renaissancer.first} ${renaissancer.last}`)); |
| 32 | + console.table(renaissancers); |
31 | 33 | // Array.prototype.map() |
32 | 34 | // 2. Give us an array of the inventory first and last names |
| 35 | + const names = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 36 | + console.log(names); |
33 | 37 |
|
34 | 38 | // Array.prototype.sort() |
35 | 39 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 40 | + const sorted_inventors = inventors.sort((a, b) => a.year - b.year); |
| 41 | + sorted_inventors.forEach(inventor => console.log(`${inventor.first} ${inventor.last} ${inventor.year}`)); |
36 | 42 |
|
37 | 43 | // Array.prototype.reduce() |
38 | 44 | // 4. How many years did all the inventors live? |
| 45 | + const accumulated_years = inventors.reduce((a, b) => a + (b.passed - b.year), 0); |
| 46 | + console.log(accumulated_years); |
39 | 47 |
|
40 | 48 | // 5. Sort the inventors by years lived |
| 49 | + const oldest = inventors.sort((a, b) => { var a_age = a.passed - a.year; var b_age = b.passed - b.year; return b_age - a_age}); |
| 50 | + console.table(oldest); |
41 | 51 |
|
42 | 52 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
43 | 53 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 54 | + const boulevards = ['yada de pee', 'poo', 'boulevard de pee']; |
| 55 | + const boulevards_de = boulevards.filter(boulevard => boulevard.includes('de')); |
44 | 56 |
|
45 | 57 |
|
46 | 58 | // 7. sort Exercise |
47 | 59 | // Sort the people alphabetically by last name |
| 60 | + const sorted_people = people.sort(); |
48 | 61 |
|
49 | 62 | // 8. Reduce Exercise |
50 | 63 | // Sum up the instances of each of these |
51 | 64 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 65 | + const transportation = data.reduce((object, item) => { |
| 66 | + object[item] = object[item] ? object[item] : 0; |
| 67 | + object[item] = object[item] + 1; |
| 68 | + return object |
| 69 | + }, {}); |
52 | 70 |
|
53 | 71 | </script> |
54 | 72 | </body> |
|
0 commit comments