|
11 | 11 | // ## Array Cardio Day 1 |
12 | 12 |
|
13 | 13 | // Some data we can work with |
| 14 | + // Each inventor is an object |
14 | 15 |
|
15 | 16 | const inventors = [ |
16 | 17 | { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
|
27 | 28 | { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 } |
28 | 29 | ]; |
29 | 30 |
|
| 31 | + 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']; |
| 32 | + |
30 | 33 | 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 | 34 |
|
32 | 35 | // Array.prototype.filter() |
33 | | - // 1. Filter the list of inventors for those who were born in the 1500's |
34 | | - const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); |
35 | | - |
36 | | - console.table(fifteen); |
| 36 | + // Use a filter function to find all inventors who were born in the 1500's |
| 37 | + // A filter function will loop over all data in the inventors array |
| 38 | + const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year <=1599)); |
| 39 | + console.table(fifteen); // table displays results for this better than log |
37 | 40 |
|
38 | 41 | // Array.prototype.map() |
39 | | - // 2. Give us an array of the inventor first and last names |
40 | | - const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 42 | + // 2. Give us an array of the inventors' first and last names |
| 43 | + const fullNames = inventors.map(inventor => inventor.first + ' ' + inventor.last); |
41 | 44 | console.log(fullNames); |
42 | 45 |
|
43 | 46 | // Array.prototype.sort() |
44 | 47 | // 3. Sort the inventors by birthdate, oldest to youngest |
45 | | - // const ordered = inventors.sort(function(a, b) { |
46 | | - // if(a.year > b.year) { |
| 48 | + // const birthdate = inventors.sort(function(a, b) { |
| 49 | + // if (a.year > b.year) { |
47 | 50 | // return 1; |
48 | 51 | // } else { |
49 | 52 | // return -1; |
50 | 53 | // } |
51 | 54 | // }); |
52 | | - |
53 | | - const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
54 | | - console.table(ordered); |
| 55 | + |
| 56 | + // shorthand way to tackle the sort function above |
| 57 | + const birthdate = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
| 58 | + console.table(birthdate); |
55 | 59 |
|
56 | 60 | // Array.prototype.reduce() |
57 | 61 | // 4. How many years did all the inventors live? |
58 | 62 | const totalYears = inventors.reduce((total, inventor) => { |
59 | 63 | return total + (inventor.passed - inventor.year); |
60 | | - }, 0); |
61 | | - |
62 | | - console.log(totalYears); |
| 64 | + }, 0); // the first total will be undefined, so add a zero here |
63 | 65 |
|
64 | 66 | // 5. Sort the inventors by years lived |
65 | 67 | const oldest = inventors.sort(function(a, b) { |
|
71 | 73 |
|
72 | 74 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
73 | 75 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
74 | | - |
75 | 76 | // const category = document.querySelector('.mw-category'); |
76 | | - // const links = Array.from(category.querySelectorAll('a')); |
| 77 | + // const links = Array.from(category.querySelector('a')); |
77 | 78 | // const de = links |
78 | | - // .map(link => link.textContent) |
79 | | - // .filter(streetName => streetName.includes('de')); |
| 79 | + // .map(link => link.textContent); |
| 80 | + // .filter(streetname => streetname.includes('de')); |
| 81 | + |
80 | 82 |
|
81 | 83 | // 7. sort Exercise |
82 | 84 | // Sort the people alphabetically by last name |
83 | | - const alpha = people.sort((lastOne, nextOne) => { |
84 | | - const [aFirst, aLast] = lastOne.split(', '); |
85 | | - const [bFirst, bLast] = nextOne.split(', '); |
86 | | - return aLast > bLast ? 1 : -1; |
| 85 | + const alpha = people.sort(function(lastOne, nextOne) { |
| 86 | + const [alast, afirst] = lastOne.split(', '); // help format the result |
| 87 | + const [blast, bfirst] = nextOne.split(', '); // help format the result |
| 88 | + // console.log(last, first); |
| 89 | + return alast > blast ? 1 : -1; |
87 | 90 | }); |
88 | 91 | console.log(alpha); |
89 | 92 |
|
90 | 93 | // 8. Reduce Exercise |
91 | 94 | // Sum up the instances of each of these |
92 | | - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogostick']; |
| 95 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'gitane', 'gunnar', 'huffy' ]; |
93 | 96 |
|
94 | 97 | const transportation = data.reduce(function(obj, item) { |
95 | | - if (!obj[item]) { |
96 | | - obj[item] = 0; |
| 98 | + // start with a blank object |
| 99 | + if(!obj[item]) { |
| 100 | + obj[item] = 0; // if there's no object item, set it to 0 |
97 | 101 | } |
98 | | - obj[item]++; |
99 | | - return obj; |
| 102 | + obj[item]++; // increment and step through |
| 103 | + return(obj); |
100 | 104 | }, {}); |
101 | 105 |
|
102 | 106 | console.log(transportation); |
|
0 commit comments