|
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=>{ |
| 42 | +if(inventor.year>=1500&&inventor.year<1600){ |
| 43 | +returntrue; |
| 44 | +} |
| 45 | +}) |
| 46 | +console.table(fifteen) |
41 | 47 |
|
42 | 48 | // Array.prototype.map() |
43 | 49 | // 2. Give us an array of the inventors first and last names |
| 50 | +constnames=inventors.map(inventor=>inventor.first+" "+inventor.last) |
| 51 | +console.log(names) |
| 52 | + |
44 | 53 |
|
45 | 54 | // Array.prototype.sort() |
46 | 55 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 56 | +constsorted=inventors.sort((a,b)=>{ |
| 57 | +returna.year-b.year |
| 58 | +}); |
| 59 | +console.table(sorted) |
47 | 60 |
|
48 | 61 | // Array.prototype.reduce() |
49 | 62 | // 4. How many years did all the inventors live all together? |
| 63 | +consttotalAge=inventors.reduce((total,inventor)=>{ |
| 64 | +returntotal+(inventor.passed-inventor.year); |
| 65 | +},0) |
| 66 | +console.log(totalAge) |
50 | 67 |
|
51 | 68 | // 5. Sort the inventors by years lived |
| 69 | +constsortedAlive=inventors.sort((a,b)=>{ |
| 70 | +constlastGuy=a.passed-a.year; |
| 71 | +constnextGuy=b.passed-b.year; |
| 72 | +returnlastGuy>nextGuy ? -1 : 1 |
| 73 | +}) |
| 74 | +console.table(sortedAlive) |
52 | 75 |
|
53 | 76 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
54 | 77 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
55 | 78 |
|
56 | 79 |
|
57 | 80 | // 7. sort Exercise |
58 | 81 | // Sort the people alphabetically by last name |
| 82 | +constsortedNames=people.sort() |
| 83 | +console.log(sortedNames) |
| 84 | + |
59 | 85 |
|
60 | 86 | // 8. Reduce Exercise |
61 | 87 | // Sum up the instances of each of these |
62 | 88 | constdata=['car','car','truck','truck','bike','walk','car','van','bike','walk','car','van','car','truck']; |
63 | 89 |
|
| 90 | +consttransportation=data.reduce(function(obj,item){ |
| 91 | +if(!obj[item]){ |
| 92 | +obj[item]=0; |
| 93 | +} |
| 94 | +obj[item]++; |
| 95 | +returnobj; |
| 96 | +},{}); |
| 97 | +console.log(transportation) |
| 98 | + |
| 99 | + |
| 100 | + |
64 | 101 | </script> |
65 | 102 | </body> |
66 | 103 | </html> |
0 commit comments