Skip to content

Commit 4354cae

Browse files
committed
finished array cardio
1 parent fefb2e1 commit 4354cae

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

File renamed without changes.
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,46 @@
2727

2828
// Array.prototype.filter()
2929
// 1. Filter the list of inventors for those who were born in the 1500's
30-
30+
const renaissancers = inventors.filter(inventor => inventor.year >= 1500 && inventor.year < 1600);
31+
renaissancers.forEach(renaissancer => console.log(`${renaissancer.first} ${renaissancer.last}`));
32+
console.table(renaissancers);
3133
// Array.prototype.map()
3234
// 2. Give us an array of the inventory first and last names
35+
const names = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
36+
console.log(names);
3337

3438
// Array.prototype.sort()
3539
// 3. Sort the inventors by birthdate, oldest to youngest
40+
const sorted_inventors = inventors.sort((a, b) => a.year - b.year);
41+
sorted_inventors.forEach(inventor => console.log(`${inventor.first} ${inventor.last} ${inventor.year}`));
3642

3743
// Array.prototype.reduce()
3844
// 4. How many years did all the inventors live?
45+
const accumulated_years = inventors.reduce((a, b) => a + (b.passed - b.year), 0);
46+
console.log(accumulated_years);
3947

4048
// 5. Sort the inventors by years lived
49+
const oldest = inventors.sort((a, b) => { var a_age = a.passed - a.year; var b_age = b.passed - b.year; return b_age - a_age});
50+
console.table(oldest);
4151

4252
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4353
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
54+
const boulevards = ['yada de pee', 'poo', 'boulevard de pee'];
55+
const boulevards_de = boulevards.filter(boulevard => boulevard.includes('de'));
4456

4557

4658
// 7. sort Exercise
4759
// Sort the people alphabetically by last name
60+
const sorted_people = people.sort();
4861

4962
// 8. Reduce Exercise
5063
// Sum up the instances of each of these
5164
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
65+
const transportation = data.reduce((object, item) => {
66+
object[item] = object[item] ? object[item] : 0;
67+
object[item] = object[item] + 1;
68+
return object
69+
}, {});
5270

5371
</script>
5472
</body>

0 commit comments

Comments
 (0)