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