|
35 | 35 | 'Berne, Eric','Berra, Yogi','Berry, Wendell','Bevan, Aneurin','Ben-Gurion, David','Bevel, Ken','Biden, Joseph','Bennington, Chester','Bierce, Ambrose', |
36 | 36 | 'Billings, Josh','Birrell, Augustine','Blair, Tony','Beecher, Henry','Biondo, Frank' |
37 | 37 | ]; |
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 | +constfifteen=inventors.filter(inventor=>inventor.year>=1500&&inventor.year<1600); |
| 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 | +constfullNames=inventors.map(inventor=>inventor.first+' '+inventor.last); |
| 47 | +console.table(fullNames); |
44 | 48 |
|
45 | 49 | // Array.prototype.sort() |
46 | 50 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 51 | +constbirthdates=inventors.sort((a,b)=>(a.year>b.year ? 1 : -1)); |
| 52 | +console.table(birthdates); |
47 | 53 |
|
48 | 54 | // Array.prototype.reduce() |
49 | 55 | // 4. How many years did all the inventors live all together? |
| 56 | +consttotalYears=inventors.reduce((total,inventor)=>{ |
| 57 | +returntotal+(inventor.passed-inventor.year); |
| 58 | +},0) |
| 59 | +console.log(totalYears); |
50 | 60 |
|
51 | 61 | // 5. Sort the inventors by years lived |
| 62 | +constsortByLife=inventors.sort((a,b)=>((a.passed-a.year)<(b.passed-b.year) ? 1 : -1)); |
| 63 | +console.table(sortByLife); |
52 | 64 |
|
53 | 65 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
54 | 66 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
55 | | - |
| 67 | +// const category = document.querySelector('.mw-category'); |
| 68 | +// const links = [...category.querySelectorAll('a')]; |
| 69 | +// const de = links |
| 70 | +// .map(link => link.textContent) |
| 71 | +// .filter(streetName => streetName.includes('de')); |
56 | 72 |
|
57 | 73 | // 7. sort Exercise |
58 | 74 | // Sort the people alphabetically by last name |
| 75 | +constsortLastname=people.sort((lastOne,nextOne)=>{ |
| 76 | +const[aLast,aFirst]=lastOne.split(', '); |
| 77 | +const[bLast,bFirst]=nextOne.split(', '); |
| 78 | +returnaLast>bLast ? 1 : -1; |
| 79 | +}); |
| 80 | +console.table(sortLastname); |
59 | 81 |
|
60 | 82 | // 8. Reduce Exercise |
61 | 83 | // Sum up the instances of each of these |
62 | 84 | constdata=['car','car','truck','truck','bike','walk','car','van','bike','walk','car','van','car','truck']; |
63 | | - |
| 85 | +consttransportation=data.reduce((obj,item)=>{ |
| 86 | +if(!obj[item]){ |
| 87 | +obj[item]=0; |
| 88 | +} |
| 89 | +obj[item]++; |
| 90 | +returnobj; |
| 91 | +},{}); |
| 92 | +console.log(transportation) |
64 | 93 | </script> |
65 | 94 | </body> |
66 | 95 | </html> |
0 commit comments