|
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 born = inventors.filter((i) => i.year < 1600 && i.year > 1499); |
| 42 | + // console.log(born); |
41 | 43 |
|
42 | 44 | // Array.prototype.map() |
43 | 45 | // 2. Give us an array of the inventors first and last names |
| 46 | + // const mappedArr = inventors.map((i) => [i.first, i.last]); |
| 47 | + // console.log(mappedArr); |
| 48 | + |
44 | 49 |
|
45 | 50 | // Array.prototype.sort() |
46 | 51 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 52 | + // const sorted = inventors.sort((a,b) => a.year > b.year ? 1 : -1); |
| 53 | + // console.table(sorted); |
47 | 54 |
|
48 | 55 | // Array.prototype.reduce() |
49 | 56 | // 4. How many years did all the inventors live all together? |
| 57 | + // const totalYears = inventors.reduce((total, inventor) => total + (inventor.passed - inventor.year),0); |
| 58 | + // console.log(totalYears); |
50 | 59 |
|
51 | 60 | // 5. Sort the inventors by years lived |
| 61 | + // const sortYears = inventors.sort((a,b) => a.passed - a.year > b.passed - b.year ? -1 : 1); |
| 62 | + // console.table(sortYears); |
52 | 63 |
|
53 | 64 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
54 | | - // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 65 | + //https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 66 | + // const category = document.querySelector(".mw-category"); |
| 67 | + // const links = [...category.querySelectorAll("a")]; |
| 68 | + // const de = links |
| 69 | + // .map(link => link.textContent) |
| 70 | + // .filter(streetName => streetName.includes('de')); |
55 | 71 |
|
56 | 72 |
|
57 | 73 | // 7. sort Exercise |
58 | 74 | // Sort the people alphabetically by last name |
| 75 | + // const sortName = people.sort((a,b) => { |
| 76 | + // const [aLast, aFirst] = a.split(', '); |
| 77 | + // const [bLast, bFirst] = b.split(', '); |
| 78 | + // return aLast > bLast ? 1 : -1; |
| 79 | + // }); |
| 80 | + // console.table(sortName); |
59 | 81 |
|
60 | 82 | // 8. Reduce Exercise |
61 | 83 | // Sum up the instances of each of these |
62 | 84 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
63 | 85 |
|
| 86 | + const transport = data.reduce((obj, item) => { |
| 87 | + if(!obj[item]){ |
| 88 | + obj[item] = 0; |
| 89 | + } |
| 90 | + obj[item]++; |
| 91 | + return obj; |
| 92 | + }, {}); |
| 93 | + console.log(transport); |
| 94 | + |
64 | 95 | </script> |
65 | 96 | </body> |
66 | 97 | </html> |
0 commit comments