|
31 | 31 |
|
32 | 32 | // Array.prototype.filter() |
33 | 33 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 34 | + const result1 = inventors.filter(investor => investor.year >= 1500 && investor.year < 1600); |
| 35 | + console.info('Result 1'); |
| 36 | + console.table(result1) |
34 | 37 |
|
35 | 38 | // Array.prototype.map() |
36 | 39 | // 2. Give us an array of the inventors first and last names |
| 40 | + const result2 = inventors.map(investor => `${investor.first} ${investor.last}`); |
| 41 | + console.info('Result 2'); |
| 42 | + console.table(result2) |
37 | 43 |
|
38 | 44 | // Array.prototype.sort() |
39 | 45 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 46 | + const result3 = inventors.sort((a, b) => { |
| 47 | + const inventorABirth = a.year; |
| 48 | + const inventorBBirth = b.year; |
| 49 | + |
| 50 | + return inventorBBirth > inventorABirth ? -1 : 1; |
| 51 | + }) |
| 52 | + console.info('Result 3'); |
| 53 | + console.table(result3); |
40 | 54 |
|
41 | 55 | // Array.prototype.reduce() |
42 | 56 | // 4. How many years did all the inventors live all together? |
| 57 | + const reducer = (accumulator, val) => { |
| 58 | + return accumulator + (val.passed - val.year) |
| 59 | + } |
| 60 | + console.info('Result 4'); |
| 61 | + console.log(inventors.reduce(reducer, 0)); |
43 | 62 |
|
44 | 63 | // 5. Sort the inventors by years lived |
| 64 | + const result5 = inventors.sort((a, b) => { |
| 65 | + const inventorALived = a.passed - a.year; |
| 66 | + const inventorBLived = b.passed - b.year; |
| 67 | + |
| 68 | + return inventorALived > inventorBLived ? 1 : -1; |
| 69 | + }) |
| 70 | + |
| 71 | + console.info('Result 5'); |
| 72 | + console.table(result5); |
45 | 73 |
|
46 | 74 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
47 | 75 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
48 | 76 |
|
49 | 77 |
|
50 | 78 | // 7. sort Exercise |
51 | 79 | // Sort the people alphabetically by last name |
| 80 | + const result7 = people.sort((a, b) => { |
| 81 | + let lastnameA = a.split(', ')[1]; |
| 82 | + let lastnameB = b.split(', ')[1]; |
| 83 | + let nameA = lastnameA.toUpperCase(); // ignore upper and lowercase |
| 84 | + let nameB = lastnameB.toUpperCase(); |
| 85 | + |
| 86 | + if (nameA < nameB) { |
| 87 | + return -1; |
| 88 | + } |
| 89 | + if (nameA > nameB) { |
| 90 | + return 1; |
| 91 | + } |
| 92 | + |
| 93 | + // names must be equal |
| 94 | + return 0; |
| 95 | + }) |
| 96 | + |
| 97 | + console.info('Result 7'); |
| 98 | + console.table(result7) |
52 | 99 |
|
53 | 100 | // 8. Reduce Exercise |
54 | 101 | // Sum up the instances of each of these |
55 | 102 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 103 | + const carReducer = (accumulator, val) => { |
| 104 | + if (!accumulator[val]) { |
| 105 | + accumulator[val] = 0; |
| 106 | + } |
| 107 | + |
| 108 | + accumulator[val] ++; |
| 109 | + |
| 110 | + return accumulator; |
| 111 | + }; |
| 112 | + |
| 113 | + const result8 = data.reduce(carReducer, {}); |
| 114 | + console.info('Result 8'); |
| 115 | + console.log(result8); |
56 | 116 |
|
57 | 117 | </script> |
58 | 118 | </body> |
|
0 commit comments