|
34 | 34 | const bornInFifteen = inventors.filter(function(person){ |
35 | 35 | return person.year > 1499 && person.year < 1600; |
36 | 36 | }); |
| 37 | + const fifteen = inventors.filter(function(inventor){ |
| 38 | + if (inventor.year >= 1500 && inventor.year < 1600) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + //if you don't return anything other than true it'll just treat it as false. |
| 42 | + }); |
| 43 | + |
| 44 | + //filter can be simplified by using the => |
| 45 | + const fifteenSimplifying = inventors.filter(inventor => inventor.year > 1499 && inventor.year < 1600); |
| 46 | + |
| 47 | + //console.table(fifteenSimplifying); |
| 48 | + //console.table(bornInFifteen); |
| 49 | + //console table outputs data to the console in tabular format |
| 50 | + |
| 51 | + |
| 52 | + |
37 | 53 |
|
38 | 54 | // Array.prototype.map() |
39 | 55 | // 2. Give us an array of the inventors' first and last names |
40 | 56 | const firstAndLast = inventors.map(function(person){ |
41 | 57 | return person.first + " " + person.last; |
42 | 58 | }); |
43 | 59 |
|
| 60 | + const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
44 | 67 | // Array.prototype.sort() |
45 | 68 | // 3. Sort the inventors by birthdate, oldest to youngest |
46 | | - inventors.sort(function(a, b){return a.year - b.year;}); |
| 69 | + //inventors.sort(function(a, b){return a.year - b.year;}); |
| 70 | + |
| 71 | + //first person is a, second person is b |
| 72 | + //const ordered = inventors.sort(function (a,b){ |
| 73 | + //if (a.year > b.year){ |
| 74 | + //return 1; |
| 75 | + //} else { |
| 76 | + //return -1; |
| 77 | + //} |
| 78 | + //}); |
| 79 | + |
| 80 | + //Using a ternarary(sp?) operator |
| 81 | + |
| 82 | + const ordered = inventors.sort((a,b) => a.year > b.year ? 1 : -1); |
| 83 | + |
| 84 | + // console.table(ordered); |
47 | 85 |
|
48 | 86 |
|
49 | 87 |
|
| 88 | + |
| 89 | + |
50 | 90 | // Array.prototype.reduce() |
51 | 91 | // 4. How many years did all the inventors live? |
52 | | - const totalYears = inventors.reduce(function(total, person){ |
53 | | - console.log (person.passed-person.year); |
54 | | - return total+(person.passed-person.year); |
55 | | - }); |
56 | | - console.log (totalYears); |
| 92 | + const totalYearsCF = inventors.reduce(function(total, inventor){ |
| 93 | + return total + (inventor.passed - inventor.year); |
| 94 | + }, 0); |
| 95 | + |
| 96 | + //console.log(totalYearsCF); |
| 97 | + |
| 98 | + const totalYears = inventors.reduce((total, inventor) => { |
| 99 | + return total + (inventor.passed - inventor.year); |
| 100 | + }, 0); |
| 101 | + |
| 102 | + //console.log (totalYears); |
| 103 | + |
| 104 | + //ZERO! You can enter another argument for the reduce function, and that effectively initializes the total var with zero. Basically var total = 0; |
| 105 | + |
| 106 | + |
57 | 107 |
|
58 | 108 | // 5. Sort the inventors by years lived |
59 | 109 |
|
| 110 | + const oldest = inventors.sort((a,b) => (a.passed-a.year) < (b.passed-b.year) ? 1 : -1); |
| 111 | + //console.table(oldest); |
| 112 | + |
| 113 | + |
60 | 114 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
61 | 115 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 116 | + //const mainWikiPage = document.querySelector(".mw-category"); |
| 117 | + //const cityNames = document.querySelectorAll(".mw-category-group ul li a"); |
| 118 | + //You don't need to do descendant selector, in this case just finding any child of class mw-category that's an anchor would do |
| 119 | + // const cityNames = Array.from(document.querySelectorAll('a')); |
| 120 | + //Array from converts a Node List into an array |
| 121 | + |
| 122 | + //const de = cityNames |
| 123 | + // .map(link => link.textContent) |
| 124 | + // .filter(streetName => streetName.includes("de") && !(streetName.includes("http"))); |
| 125 | + |
62 | 126 |
|
63 | 127 |
|
64 | 128 | // 7. sort Exercise |
65 | 129 | // Sort the people alphabetically by last name |
| 130 | + const alpha = people.sort((lastOne, nextOne) =>{ |
| 131 | + const [aLast, aFirst] = lastOne.split(', '); |
| 132 | + const [bLast, bFirst] = nextOne.split(', '); |
| 133 | + return aLast > bLast ? 1 : -1; |
| 134 | + }); |
| 135 | + //console.log(alpha); |
66 | 136 |
|
67 | 137 | // 8. Reduce Exercise |
68 | 138 | // Sum up the instances of each of these |
69 | 139 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
70 | 140 |
|
| 141 | + const transportation = data.reduce((obj, item) => { |
| 142 | + if (!obj[item]) { |
| 143 | + obj[item] = 0; |
| 144 | + } |
| 145 | + obj[item]++; |
| 146 | + return obj; |
| 147 | + },{}); |
| 148 | + |
| 149 | + console.log(transportation); |
| 150 | + |
| 151 | + |
71 | 152 | </script> |
72 | 153 | </body> |
73 | 154 | </html> |
0 commit comments