Skip to content

Commit d48d753

Browse files
authored
Update index-FINISHED.html
1 parent fcdd10b commit d48d753

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

04 - Array Cardio Day 1/index-FINISHED.html

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// ## Array Cardio Day 1
1212

1313
// Some data we can work with
14+
// Each inventor is an object
1415

1516
const inventors = [
1617
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
@@ -27,39 +28,40 @@
2728
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
2829
];
2930

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+
3033
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'];
3134

3235
// 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
3740

3841
// 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);
4144
console.log(fullNames);
4245

4346
// Array.prototype.sort()
4447
// 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) {
4750
// return 1;
4851
// } else {
4952
// return -1;
5053
// }
5154
// });
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);
5559

5660
// Array.prototype.reduce()
5761
// 4. How many years did all the inventors live?
5862
const totalYears = inventors.reduce((total, inventor) => {
5963
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
6365

6466
// 5. Sort the inventors by years lived
6567
const oldest = inventors.sort(function(a, b) {
@@ -71,32 +73,34 @@
7173

7274
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
7375
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
74-
7576
// const category = document.querySelector('.mw-category');
76-
// const links = Array.from(category.querySelectorAll('a'));
77+
// const links = Array.from(category.querySelector('a'));
7778
// 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+
8082

8183
// 7. sort Exercise
8284
// 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;
8790
});
8891
console.log(alpha);
8992

9093
// 8. Reduce Exercise
9194
// 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' ];
9396

9497
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
97101
}
98-
obj[item]++;
99-
return obj;
102+
obj[item]++; // increment and step through
103+
return(obj);
100104
}, {});
101105

102106
console.log(transportation);

0 commit comments

Comments
 (0)