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