|
31 | 31 |
|
32 | 32 | 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']; |
33 | 33 |
|
| 34 | + const streets = ['Boulevards of the Marshals', |
| 35 | + 'Boulevard Auguste-Blanqui', |
| 36 | + 'Boulevard Barbès', |
| 37 | + 'Boulevard Beaumarchais', |
| 38 | + 'Boulevard de l\'Amiral-Bruix', |
| 39 | + 'Boulevard de Strasbourg', |
| 40 | + 'Boulevard des Capucines', |
| 41 | + 'Boulevard de la Chapelle', |
| 42 | + 'Boulevard de Clichy', |
| 43 | + 'Boulevard du Crime', |
| 44 | + 'Boulevard Haussmann', |
| 45 | + 'Boulevard de l\'Hôpital', |
| 46 | + 'Boulevard des Italiens', |
| 47 | + 'Boulevard de la Madeleine', |
| 48 | + 'Boulevard de Magenta', |
| 49 | + 'Boulevard Montmartre', |
| 50 | + 'Boulevard du Montparnasse', |
| 51 | + 'Boulevard Raspail', |
| 52 | + 'Boulevard Richard-Lenoir', |
| 53 | + 'Boulevard de Rochechouart', |
| 54 | + 'Boulevard Saint-Germain', |
| 55 | + 'Boulevard Saint-Michel', |
| 56 | + 'Boulevard de Sébastopol', |
| 57 | + 'Boulevard du Temple', |
| 58 | + 'Boulevard Voltaire', |
| 59 | + 'Boulevard de la Zone']; |
34 | 60 | // Array.prototype.filter() |
35 | 61 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 62 | + const i1500s = inventors.filter(inventor => inventor.year >= 1500 && inventor.year < 1600); |
| 63 | + console.table(i1500s); |
36 | 64 |
|
37 | 65 | // Array.prototype.map() |
38 | 66 | // 2. Give us an array of the inventors' first and last names |
| 67 | + const names = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 68 | + console.log(names); |
39 | 69 |
|
40 | 70 | // Array.prototype.sort() |
41 | 71 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 72 | + const sorted = inventors.sort((a, b) => (a.year > b.year) ? 1 : -1); |
| 73 | + console.table(sorted); |
42 | 74 |
|
43 | 75 | // Array.prototype.reduce() |
44 | 76 | // 4. How many years did all the inventors live? |
| 77 | + const years = inventors.reduce((a, b) => a + (b.passed - b.year),0); |
| 78 | + console.log(years,'cumulative years'); |
45 | 79 |
|
46 | 80 | // 5. Sort the inventors by years lived |
| 81 | + inventors.forEach(inventor => inventor.age = (inventor.passed - inventor.year)); |
| 82 | + const oldest = inventors.sort((a,b) => (a.age > b.age) ? -1 : 1); |
| 83 | + |
| 84 | + console.table(oldest); |
47 | 85 |
|
48 | 86 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
49 | 87 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
50 | | - |
| 88 | + const de = streets.filter(street => street.match('de')); |
| 89 | + console.log(de,'de streets', `${de.length} total`); |
51 | 90 |
|
52 | 91 | // 7. sort Exercise |
53 | 92 | // Sort the people alphabetically by last name |
| 93 | + const alpha = people.sort(function(a,b){ |
| 94 | + const [aLast, aFirst] = a.split(', '); |
| 95 | + const [bLast, bFirst] = a.split(', '); |
| 96 | + return (aLast > bLast) ? 1 : -1; |
| 97 | + }); |
| 98 | + console.log(alpha); |
| 99 | + // sort by first name |
| 100 | + const first = people.sort(function(a,b){ |
| 101 | + const aFirst = a.split(' ')[1]; |
| 102 | + const bFirst = b.split(' ')[1]; |
| 103 | + return (aFirst < bFirst) ? -1 : 1; |
| 104 | + }); |
| 105 | + console.log(first); |
54 | 106 |
|
55 | 107 | // 8. Reduce Exercise |
56 | 108 | // Sum up the instances of each of these |
57 | 109 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
58 | 110 |
|
| 111 | + const transport = data.reduce(function(obj, item){ |
| 112 | + if(obj.hasOwnProperty(item)) { |
| 113 | + obj[item]++ |
| 114 | + } else { |
| 115 | + obj[item] = 1; |
| 116 | + } |
| 117 | + return obj; |
| 118 | + }, {}); |
| 119 | + console.log(transport); |
| 120 | + |
| 121 | + const altTransport = data.reduce(function(obj, item){ |
| 122 | + if(!obj[item]) { |
| 123 | + obj[item] = 0; |
| 124 | + } |
| 125 | + obj[item]++; |
| 126 | + return obj; |
| 127 | + },{}); |
| 128 | + console.log(altTransport); |
| 129 | + |
59 | 130 | </script> |
60 | 131 | </body> |
61 | 132 | </html> |
0 commit comments