|
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 | | - |
| 34 | + let before1500 = inventors.filter(x => x.year > 1500 && x.year < 1600); |
| 35 | + console.table(before1500) |
35 | 36 | // Array.prototype.map() |
36 | 37 | // 2. Give us an array of the inventors' first and last names |
37 | | - |
| 38 | + let firstAndLastNames = inventors.map(x => [x.first, x.last]); |
| 39 | + console.log(firstAndLastNames); |
38 | 40 | // Array.prototype.sort() |
39 | 41 | // 3. Sort the inventors by birthdate, oldest to youngest |
40 | | - |
| 42 | + let sorted = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
| 43 | + console.table(sorted); |
41 | 44 | // Array.prototype.reduce() |
42 | 45 | // 4. How many years did all the inventors live? |
| 46 | + let reduced = inventors.reduce((a, b) => a + (b.passed - b.year), 0); |
| 47 | + console.log(reduced); |
43 | 48 |
|
44 | 49 | // 5. Sort the inventors by years lived |
45 | | - |
| 50 | + let sortedByYearsLived = inventors.sort((a, b) => (a.passed - a.year) > (b.passed - b.year) ? 1: -1); |
| 51 | + console.table(sortedByYearsLived); |
46 | 52 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
47 | 53 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 54 | + [...document.querySelectorAll('.mw-category a')].map(node => node.innerHTML).filter(text => text.includes('de')) |
| 55 | + // nah i dont wanna get list of paris names. i'm going to instead all people with "beck" in its name |
48 | 56 |
|
| 57 | + |
| 58 | + let beckPeople = people.filter(x => x.toLowerCase().includes('beck')); |
| 59 | + console.log('beckpeople', beckPeople); |
49 | 60 |
|
50 | 61 | // 7. sort Exercise |
51 | 62 | // Sort the people alphabetically by last name |
| 63 | + let sortedByLastName = people.sort(); |
| 64 | + console.log('lastname sort', sortedByLastName); |
| 65 | + let sortedByFirstName = people.sort((a, b) => a.split(',')[1] > b.split(',')[1] ? 1 : -1); |
| 66 | + console.log('firstname', sortedByFirstName); |
52 | 67 |
|
53 | 68 | // 8. Reduce Exercise |
54 | 69 | // Sum up the instances of each of these |
55 | 70 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
56 | | - |
| 71 | + let dataCount = data.reduce((count, curr) => { |
| 72 | + if (!count[curr]) { |
| 73 | + count[curr] = 1 |
| 74 | + } else { |
| 75 | + count[curr] ++; |
| 76 | + } |
| 77 | + return count; |
| 78 | + }, {}); |
| 79 | + console.table(dataCount); |
57 | 80 | </script> |
58 | 81 | </body> |
59 | 82 | </html> |
0 commit comments