|
28 | 28 | // Array.prototype.filter() |
29 | 29 | // 1. Filter the list of inventors for those who were born in the 1500's |
30 | 30 |
|
| 31 | + const fifteenCenturyInventors = inventors.filter(function(inventor){ |
| 32 | + if(inventor.year > 1500 && inventor.year <1600) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + }); |
| 36 | + console.table(fifteenCenturyInventors); |
| 37 | + |
31 | 38 | // Array.prototype.map() |
32 | 39 | // 2. Give us an array of the inventory first and last names |
| 40 | + const inventorNames = inventors.map(function(inventor){ |
| 41 | + return inventor.first + " " + inventor.last; |
| 42 | + }); |
| 43 | + |
| 44 | + console.log(inventorNames); |
33 | 45 |
|
34 | 46 | // Array.prototype.sort() |
35 | 47 | // 3. Sort the inventors by birthdate, oldest to youngest |
36 | 48 |
|
| 49 | + const sortedbyBirthdate = inventors.sort(function(a,b){ |
| 50 | + return a.year > b.year; |
| 51 | + }); |
| 52 | + |
| 53 | + console.table(sortedbyBirthdate); |
| 54 | + |
37 | 55 | // Array.prototype.reduce() |
38 | 56 | // 4. How many years did all the inventors live? |
39 | 57 |
|
| 58 | + const yearsLived = inventors.reduce(function(acc,curr){ |
| 59 | + return acc + (curr.passed - curr.year); |
| 60 | + },0); |
| 61 | + |
| 62 | + console.log(yearsLived); |
| 63 | + |
40 | 64 | // 5. Sort the inventors by years lived |
41 | 65 |
|
| 66 | + const sortedByYearsLived = inventors.sort(function(a,b){ |
| 67 | + return (a.passed - a.year) < (b.passed - b.year); |
| 68 | + }); |
| 69 | + console.table(sortedByYearsLived); |
| 70 | + |
42 | 71 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
43 | 72 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
44 | 73 |
|
45 | 74 |
|
46 | 75 | // 7. sort Exercise |
47 | 76 | // Sort the people alphabetically by last name |
48 | 77 |
|
| 78 | + const sortedByLastName = people.sort(function(a,b){ |
| 79 | + return a > b; |
| 80 | + }) |
| 81 | + |
| 82 | + console.log(sortedByLastName); |
| 83 | + |
49 | 84 | // 8. Reduce Exercise |
50 | 85 | // Sum up the instances of each of these |
51 | 86 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
52 | 87 |
|
| 88 | + const transportation = data.reduce(function(obj,item){ |
| 89 | + if(!obj[item]){ |
| 90 | + obj[item] = 0; |
| 91 | + } |
| 92 | + |
| 93 | + obj[item]++; |
| 94 | + return obj; |
| 95 | + |
| 96 | + },{}); |
| 97 | + console.log(transportation); |
53 | 98 | </script> |
54 | 99 | </body> |
55 | 100 | </html> |
0 commit comments