|
11 | 11 | // ## Array Cardio Day 1 |
12 | 12 |
|
13 | 13 | // Some data we can work with |
14 | | - |
15 | | - const inventors = [ |
| 14 | + let inventors = [ |
16 | 15 | { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
17 | 16 | { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, |
18 | 17 | { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, |
|
27 | 26 | { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 } |
28 | 27 | ]; |
29 | 28 |
|
30 | | - const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William']; |
31 | 29 |
|
32 | 30 | // Array.prototype.filter() |
33 | 31 | // 1. Filter the list of inventors for those who were born in the 1500's |
34 | | - |
35 | | - const fifteens = inventors.filter(i => i.year > 1500 && i.year < 1600); |
36 | | - //console.table(fifteens); |
| 32 | + const inventorsOfFifteens = inventors.filter(inventor => |
| 33 | + inventor.year >= 1500 && inventor.year < 1600 |
| 34 | + ) |
| 35 | + // console.table(inventorsOfFifteens) |
37 | 36 |
|
38 | 37 |
|
39 | 38 | // Array.prototype.map() |
40 | 39 | // 2. Give us an array of the inventors' first and last names |
| 40 | + const inventorsNames = inventors.map(i => `${i.first} ${i.last}`) |
| 41 | + // console.table(inventorsNames) |
41 | 42 |
|
42 | | - const list = inventors.map(i => `${i.first} ${i.last}`); |
43 | | - //console.log(list); |
44 | | - |
45 | 43 |
|
46 | 44 | // Array.prototype.sort() |
47 | 45 | // 3. Sort the inventors by birthdate, oldest to youngest |
48 | | - |
49 | | - const inventorsByYear = inventors.sort((a, b) => a.year > b.year ? 1 : -1 ) |
50 | | - //console.table(inventorsByYear); |
| 46 | + const inventorsSortedByBirthday = inventors.sort((a, b) => { |
| 47 | + const aBirthday = (a.year) |
| 48 | + const bBirthday = (b.year) |
| 49 | + return aBirthday > bBirthday ? 1 : -1 |
| 50 | + } |
| 51 | + ) |
| 52 | + // console.table(inventorsSortedByBirthday) |
51 | 53 |
|
52 | 54 |
|
53 | 55 | // Array.prototype.reduce() |
54 | 56 | // 4. How many years did all the inventors live? |
55 | | - const totalYears = inventors.reduce(((total, i) => (i.passed - i.year) + total), 0); |
56 | | - //console.log(totalYears); |
| 57 | + const inventorsTotalAge = inventors.reduce((total, i) => total + (i.passed - i.year), 0) |
| 58 | + // console.table(inventorsTotalAge) |
57 | 59 |
|
58 | 60 |
|
59 | 61 | // 5. Sort the inventors by years lived |
| 62 | + const inventorsSortedByAge = inventors.sort((a, b) => { |
| 63 | + const aAge = (a.passed - a.year) |
| 64 | + const bAge = (b.passed - b.year) |
| 65 | + return aAge > bAge ? -1 : 1 |
| 66 | + } |
| 67 | + ).map(i => { |
| 68 | + const fullData = {...i} |
| 69 | + fullData.age = (i.passed - i.year) |
| 70 | + return fullData |
| 71 | + }) |
| 72 | + // console.table(inventorsSortedByAge) |
60 | 73 |
|
61 | | - const inventorsByAge = inventors |
62 | | - .sort((a, b) => (a.passed - a.year) > (b.passed - b.year) ? -1 : 1) |
63 | | - .map(i => { |
64 | | - const newObj = { |
65 | | - ...i, |
66 | | - age: (i.passed - i.year) |
67 | | - } |
68 | | - return newObj |
69 | | - }); |
70 | | - console.table(inventorsByAge); |
71 | 74 |
|
72 | 75 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
73 | 76 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
74 | | - |
| 77 | + const blvdList = Array.from(document.querySelectorAll('.mw-category-group li a')) |
| 78 | + const deList = blvdList.map(b => b.innerText).filter(blvd => blvd.includes('de')) |
| 79 | + |
75 | 80 |
|
76 | 81 | // 7. sort Exercise |
77 | 82 | // Sort the people alphabetically by last name |
78 | | - |
79 | | - const peopleByLast = people.sort((a, b) => { |
80 | | - const [aLast, aFirst] = a.split(', '); |
81 | | - const [bLast, bFirst] = b.split(', '); |
| 83 | + const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William']; |
| 84 | + const peopleSorted = people.sort((a,b) => { |
| 85 | + const [aLast, aFirst] = a.split(', ') |
| 86 | + const [bLast, bFirst] = b.split(', ') |
82 | 87 | return aLast > bLast ? 1 : -1 |
83 | 88 | }) |
84 | | - console.log(peopleByLast); |
| 89 | + // console.log(peopleSorted) |
85 | 90 |
|
86 | 91 |
|
87 | 92 | // 8. Reduce Exercise |
88 | 93 | // Sum up the instances of each of these |
89 | 94 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
90 | | - |
91 | | - const transportation = data.reduce(function(obj, item) { |
92 | | - if (!obj[item]) { |
93 | | - obj[item] = 0; |
| 95 | + const sum = data.reduce((obj, i) => { |
| 96 | + if(!obj[i]) { |
| 97 | + obj[i] = 0 |
94 | 98 | } |
95 | | - obj[item]++; |
96 | | - return obj; |
| 99 | + obj[i]++ |
| 100 | + return obj |
97 | 101 | }, {}) |
98 | | - |
99 | | - console.log(transportation); |
100 | | - |
| 102 | + console.log(sum) |
101 | 103 | </script> |
102 | 104 | </body> |
103 | 105 | </html> |
0 commit comments