|
21 | 21 | { first: 'Max', last: 'Planck', year: 1858, passed: 1947 } |
22 | 22 | ]; |
23 | 23 |
|
24 | | - const flavours = ['Chocolate Chip', 'Kulfi', 'Caramel Praline', 'Chocolate', 'Burnt Caramel', 'Pistachio', 'Rose', 'Sweet Coconut', 'Lemon Cookie', 'Toffeeness', 'Toasted Almond', 'Black Raspberry Crunch', 'Chocolate Brownies', 'Pistachio Almond', 'Strawberry', 'Lavender Honey', 'Lychee', 'Peach', 'Black Walnut', 'Birthday Cake', 'Mexican Chocolate', 'Mocha Almond Fudge', 'Raspberry']; |
25 | | - |
26 | | - 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']; |
27 | | - |
28 | 24 | // Array.prototype.filter() |
29 | 25 | // 1. Filter the list of inventors for those who were born in the 1500's |
30 | 26 |
|
| 27 | + const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year <= 1600)); |
| 28 | + // console.table(fifteen); |
| 29 | + |
31 | 30 | // Array.prototype.map() |
32 | 31 | // 2. Give us an array of the inventory first and last names |
33 | 32 |
|
| 33 | + const fullName = inventors.map(inventor => (inventor.first + ' ' +inventor.last)); |
| 34 | + // console.log(fullName); |
| 35 | + |
| 36 | + const inventorName = inventors.map(inventor => inventor.first + ' ' + inventor.last); |
| 37 | + // console.log(inventorName); |
| 38 | + |
| 39 | + |
34 | 40 | // Array.prototype.sort() |
35 | 41 | // 3. Sort the inventors by birthdate, oldest to youngest |
36 | 42 |
|
| 43 | + const birth = inventors.sort(function(a, b) { |
| 44 | + if (a.year >= b.year) { |
| 45 | + return 1; |
| 46 | + } else { |
| 47 | + return -1; |
| 48 | + } |
| 49 | + }); |
| 50 | + // console.table(birth); |
| 51 | + |
| 52 | + |
| 53 | +//Short Hand |
| 54 | + const birthYear = inventors.sort((a, b) => a.year >= b.year ? 1 : -1); |
| 55 | + // console.table(birthYear); |
| 56 | + |
| 57 | + |
37 | 58 | // Array.prototype.reduce() |
38 | 59 | // 4. How many years did all the inventors live? |
| 60 | + const totalYears = inventors.reduce((total, inventor) => { |
| 61 | + return total + (inventor.passed - inventor.year); |
| 62 | + }, .0 ); |
| 63 | + // console.log(totalYears); |
39 | 64 |
|
40 | 65 | // 5. Sort the inventors by years lived |
| 66 | + const oldest = inventors.sort((a,b) => { |
| 67 | + const lastGuy = a.passed - a.year; |
| 68 | + const nextGuy = b.passed - b.year; |
| 69 | + return nextGuy > lastGuy ? 1 : -1; |
| 70 | + }); |
| 71 | + // console.table(oldest); |
41 | 72 |
|
42 | 73 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
43 | 74 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 75 | + // const category = document.querySelector('.mw-category'); |
| 76 | + // const links = Array.from(category.querySelectorAll('a')); |
| 77 | + // |
| 78 | + // const de = links |
| 79 | + // .map(link => link.textContent) |
| 80 | + // .filter(streetName => streetName.includes('de')); |
| 81 | + |
| 82 | + |
44 | 83 |
|
| 84 | + 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']; |
45 | 85 |
|
46 | 86 | // 7. sort Exercise |
47 | 87 | // Sort the people alphabetically by last name |
| 88 | + const lastName= people.sort(function(lastOne, nextOne) { |
| 89 | + const [aLast, aFirst] = lastOne.split (', '); |
| 90 | + const [bLast, bFirst] = nextOne.split (', '); |
| 91 | + return aLast > bLast ? 1 : -1; |
| 92 | + }); |
| 93 | + // console.log(lastName); |
| 94 | + |
| 95 | + |
48 | 96 |
|
49 | 97 | // 8. Reduce Exercise |
50 | 98 | // Sum up the instances of each of these |
51 | | - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 99 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'motorcycle', 'van', 'bike', 'walk', 'motorcycle', 'car', 'van', 'car', 'truck', 'hoverBoard' ]; |
| 100 | + |
| 101 | + const transportation = data.reduce(function(obj,item){ |
| 102 | + if(!obj[item]){ |
| 103 | + obj[item] = 0; |
| 104 | + } |
| 105 | + obj[item]++; |
| 106 | + return obj; |
| 107 | + }, {}); |
| 108 | + console.log(transportation); |
52 | 109 |
|
53 | 110 | </script> |
54 | 111 | </body> |
|
0 commit comments