|
31 | 31 |
|
32 | 32 | // Array.prototype.filter() |
33 | 33 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 34 | + const fifteen = inventors.filter(function(i){ |
| 35 | + return i.year >= 1500 && i.year < 1600; |
| 36 | + }); |
| 37 | + |
| 38 | + // Can be written as |
| 39 | + //const fifteen = inventors.filter(i => i.year >= 1500 && i.year < 1600); |
| 40 | + console.table(fifteen); |
34 | 41 |
|
35 | 42 | // Array.prototype.map() |
36 | 43 | // 2. Give us an array of the inventors' first and last names |
| 44 | + const fullNames = inventors.map(i => `${i.first} ${i.last}`); |
| 45 | + console.log(fullNames); |
37 | 46 |
|
38 | 47 | // Array.prototype.sort() |
39 | 48 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 49 | + // const ordered = inventors.sort(function(a, b){ |
| 50 | + // return a.year - b.year; |
| 51 | + // }); |
| 52 | + |
| 53 | + const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
| 54 | + console.table(ordered); |
40 | 55 |
|
41 | 56 | // Array.prototype.reduce() |
42 | 57 | // 4. How many years did all the inventors live? |
| 58 | + const totalYears = inventors.reduce((total, inventor) => { |
| 59 | + return total + (inventor.passed - inventor.year); |
| 60 | + }, 0); |
| 61 | + console.log(totalYears); |
43 | 62 |
|
44 | 63 | // 5. Sort the inventors by years lived |
| 64 | + const sortedByYearsLived = inventors.sort(function(a, b){ |
| 65 | + const lastGuy = a.passed - a.year; |
| 66 | + const nextGuy = b.passed - b.year; |
| 67 | + return lastGuy > nextGuy ? -1 : 1; |
| 68 | + }); |
| 69 | + console.table(sortedByYearsLived); |
45 | 70 |
|
46 | 71 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
47 | 72 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
48 | 73 |
|
| 74 | + // const category = document.querySelector('.mw-category'); |
| 75 | + // const links = Array.from(category.querySelectorAll('a')); |
| 76 | + // const de = links |
| 77 | + // .map(link => link.textContent) |
| 78 | + // .filter(streetName => streetName.includes('de')); |
49 | 79 |
|
50 | 80 | // 7. sort Exercise |
51 | 81 | // Sort the people alphabetically by last name |
| 82 | + const alpha = people.sort((a, b) => { |
| 83 | + const [aLast, aFirst] = a.split(', '); |
| 84 | + const [bLast, bFirst] = b.split(', '); |
| 85 | + |
| 86 | + return aLast > bLast ? 1 : -1; |
| 87 | + }); |
| 88 | + console.log(alpha); |
52 | 89 |
|
53 | 90 | // 8. Reduce Exercise |
54 | 91 | // Sum up the instances of each of these |
55 | 92 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 93 | + const transportation = data.reduce(function(obj, item){ |
| 94 | + if(!obj[item]){ |
| 95 | + obj[item] = 0; |
| 96 | + } |
| 97 | + obj[item]++; |
| 98 | + return obj; |
| 99 | + }, {}); |
| 100 | + console.log(transportation); |
56 | 101 |
|
57 | 102 | </script> |
58 | 103 | </body> |
|
0 commit comments