|
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(function(inventor) { |
| 41 | + if(inventor.year >= 1500 && inventor.year <= 1600) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + }) |
| 45 | + // console.table(fifteen); |
| 46 | + // console.log(fifteen) |
40 | 47 |
|
41 | 48 | // Array.prototype.map()
|
42 | 49 | // 2. Give us an array of the inventors first and last names
|
| 50 | + const fullName = inventors.map(inventor => inventor.first + ' ' + inventor.last) |
| 51 | + // console.log(fullName) |
| 52 | + // console.table(fullName); |
43 | 53 |
|
44 | 54 | // Array.prototype.sort()
|
45 | 55 | // 3. Sort the inventors by birthdate, oldest to youngest
|
| 56 | + // const sortBirthDate = inventors.sort(function(a, b) { |
| 57 | + // if(a.year >b.year) { |
| 58 | + // return 1; |
| 59 | + // } else { |
| 60 | + // return -1; |
| 61 | + // } |
| 62 | + // }) |
| 63 | + |
| 64 | + const sortBirthDate = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
| 65 | + // console.table(sortBirthDate) |
| 66 | + // console.log(sortBirthDate) |
46 | 67 |
|
47 | 68 | // Array.prototype.reduce()
|
48 | 69 | // 4. How many years did all the inventors live all together?
|
| 70 | + const totalYears = inventors.reduce((total, inventor) => { |
| 71 | + return total + (inventor.passed - inventor.year); |
| 72 | + }, 0) |
| 73 | + // console.log(totalYears) |
49 | 74 |
|
50 | 75 | // 5. Sort the inventors by years lived
|
| 76 | + const oldest = inventors.sort(function(a, b) { |
| 77 | + const lastGuy = a.passed - a.year; |
| 78 | + const nextGuy = b.passed - b.year; |
| 79 | + return lastGuy > nextGuy ? -1 : 1; |
| 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')) |
| 92 | + // const de = links |
| 93 | + // .map(link => link.textContent) |
| 94 | + // .filter(streetName => streetName.includes('de')); |
| 95 | + |
54 | 96 |
|
55 | 97 |
|
56 | 98 | // 7. sort Exercise
|
57 | 99 | // Sort the people alphabetically by last name
|
| 100 | + const alpha = people.sort((lastOne, nextOne) => { |
| 101 | + const [aLast, aFirst] = lastOne.split(', ') |
| 102 | + const [bLast, bFirst] = nextOne.split(', ') |
| 103 | + return aLast > bLast ? 1 : -1; |
| 104 | + }) |
| 105 | + // console.log(alpha) |
58 | 106 |
|
59 | 107 | // 8. Reduce Exercise
|
60 | 108 | // Sum up the instances of each of these
|
61 | 109 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
| 110 | + const transportation = data.reduce(function(obj, item) { |
| 111 | + if(!obj[item]) { |
| 112 | + obj[item] = 0; |
| 113 | + } |
| 114 | + obj[item]++; |
| 115 | + return obj; |
| 116 | + }, {}) |
62 | 117 |
|
63 | 118 | </script>
|
64 | 119 | </body>
|
|
0 commit comments