|
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 | + console.table(inventors.filter(item => item.year > 1500 && item.year < 1600)); |
| 31 | + |
31 | 32 | // Array.prototype.map() |
32 | 33 | // 2. Give us an array of the inventory first and last names |
33 | | - |
| 34 | + console.log(inventors.map(item => `${item.first} ${item.last}` )); |
34 | 35 | // Array.prototype.sort() |
35 | 36 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 37 | + console.table(inventors.sort((a, b) => a.year - b.year)); |
36 | 38 |
|
37 | 39 | // Array.prototype.reduce() |
38 | 40 | // 4. How many years did all the inventors live? |
39 | | - |
| 41 | + console.log(inventors.reduce((total, inventor) => (total += (inventor.passed - inventor.year) ), 0 )) ; |
40 | 42 | // 5. Sort the inventors by years lived |
| 43 | + console.table(inventors.sort((a, b) => { |
| 44 | + return (a.passed - a.year) < (b.passed - b.year) ? 1 : -1; |
| 45 | + })) ; |
41 | 46 |
|
42 | 47 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
43 | 48 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
44 | | - |
45 | | - |
| 49 | + links = [...document.querySelectorAll('.mw-category a')] |
| 50 | + links = Array.from(document.querySelectorAll('.mw-category a')); |
| 51 | + de = links.map(link => link.textContent) |
| 52 | + .filter(item => item.indexOf('de') > -1); |
| 53 | + console.log(de); |
46 | 54 | // 7. sort Exercise |
47 | 55 | // Sort the people alphabetically by last name |
48 | | - |
| 56 | + console.log(people.sort((a,b) =>{ |
| 57 | + const [aLast, aFirst] = a.split(', '); |
| 58 | + const [bLast, bFirst] = b.split(', '); |
| 59 | + return aLast > bLast ? 1 : -1; |
| 60 | + })) |
49 | 61 | // 8. Reduce Exercise |
50 | 62 | // Sum up the instances of each of these |
51 | 63 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
52 | | - |
| 64 | + console.log(data.reduce((obj, item) => { |
| 65 | + if (!obj[item]) obj[item] = 0; |
| 66 | + obj[item]++; |
| 67 | + return obj |
| 68 | + },{})); |
53 | 69 | </script> |
54 | 70 | </body> |
55 | 71 | </html> |
0 commit comments