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