Skip to content

Commit 6736d58

Browse files
author
Kyle Bradshaw
committed
array cardio v1 kdb
1 parent 62b528b commit 6736d58

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

04 - Array Cardio Day 1/index-START.html renamed to 04 - Array Cardio Day 1/index.html

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,102 @@
3131

3232
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'];
3333

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'];
3460
// Array.prototype.filter()
3561
// 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);
3664

3765
// Array.prototype.map()
3866
// 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);
3969

4070
// Array.prototype.sort()
4171
// 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);
4274

4375
// Array.prototype.reduce()
4476
// 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');
4579

4680
// 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);
4785

4886
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4987
// 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`);
5190

5291
// 7. sort Exercise
5392
// 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);
54106

55107
// 8. Reduce Exercise
56108
// Sum up the instances of each of these
57109
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
58110

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+
59130
</script>
60131
</body>
61132
</html>

0 commit comments

Comments
 (0)