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