|
29 | 29 |
|
30 | 30 | 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']; |
31 | 31 |
|
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']; |
| 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 | 34 | // Array.prototype.filter() |
35 | 35 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 36 | + const result = inventors.filter(function(inventor) { |
| 37 | + if (inventor.year >1500 && inventor.year<=1599) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + console.table(result); |
36 | 43 |
|
37 | 44 | // Array.prototype.map() |
38 | 45 | // 2. Give us an array of the inventors' first and last names |
| 46 | + const inventorFirstAndLast = inventors.map(function(inventor, item) { |
| 47 | + return `${inventor.first} ${inventor.last}`; |
| 48 | + }); |
| 49 | + |
| 50 | + console.log(inventorFirstAndLast); |
39 | 51 |
|
40 | 52 | // Array.prototype.sort() |
41 | 53 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 54 | + const sorted = inventors.sort(function (first, last) { |
| 55 | + /* body... */ |
| 56 | + if (first.year > last.year) { |
| 57 | + return 1; |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + console.table(sorted); |
42 | 62 |
|
43 | 63 | // Array.prototype.reduce() |
44 | 64 | // 4. How many years did all the inventors live? |
45 | | - |
| 65 | + const total = inventors.reduce(function(total, inventor) { |
| 66 | + return total + (inventor.passed - inventor.year); |
| 67 | + }, 0); |
| 68 | + console.log(total); |
46 | 69 | // 5. Sort the inventors by years lived |
| 70 | + const sortByAge = inventors.sort(function(a,b) { |
| 71 | + const ageOfFirst = a.passed - a.year; |
| 72 | + const ageOfLast = b.passed - b.year; |
| 73 | + if (ageOfFirst > ageOfLast) { |
| 74 | + return 1; |
| 75 | + } |
| 76 | + }); |
| 77 | + console.table(sortByAge); |
47 | 78 |
|
48 | 79 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
49 | 80 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
50 | | - |
| 81 | + /*const category = document.querySelector('.mw-category'); |
| 82 | + const links = Array.from(category.querySelectorAll('.mw-category a')); |
| 83 | + const streetNames = links.map(function(index, elem) { |
| 84 | + const linkContent = index.innerText; |
| 85 | + return linkContent; |
| 86 | + }); |
| 87 | + const filteredNames = streetNames.filter(function(streetName) { |
| 88 | + return streetName.includes('de'); |
| 89 | + }); |
| 90 | + console.log(filteredNames);*/ |
51 | 91 |
|
52 | 92 | // 7. sort Exercise |
53 | 93 | // Sort the people alphabetically by last name |
| 94 | + const alphaSorted = people.sort(function(firstOne, nextOne) { |
| 95 | + |
| 96 | + const [alast, afirst] = firstOne.split(', '); |
| 97 | + const [blast, bfirst] = nextOne.split(', '); |
| 98 | + return alast > blast ? 1 : -1; |
| 99 | + }); |
| 100 | + console.log(alphaSorted); |
54 | 101 |
|
55 | 102 | // 8. Reduce Exercise |
56 | 103 | // Sum up the instances of each of these |
57 | | - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 104 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'varun','varun']; |
| 105 | + |
| 106 | + const summedUp = data.reduce(function(obj, item){ |
| 107 | + if(!obj[item]){ |
| 108 | + obj[item] = 0; |
| 109 | + } |
| 110 | + |
| 111 | + obj[item]++; |
| 112 | + return obj; |
| 113 | + |
| 114 | + },{}); |
| 115 | + |
| 116 | + console.log(summedUp); |
58 | 117 |
|
59 | 118 | </script> |
60 | 119 | </body> |
|
0 commit comments