Skip to content

Commit f871145

Browse files
committed
Finnish Array Cardio day 1
1 parent 6b98a14 commit f871145

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,53 @@
3838

3939
// Array.prototype.filter()
4040
// 1. Filter the list of inventors for those who were born in the 1500's
41+
const e1 = inventors.filter(inventor => inventor.year >= 1500 && inventor.year < 1600);
42+
console.table(e1);
4143

4244
// Array.prototype.map()
4345
// 2. Give us an array of the inventors first and last names
46+
const e2 = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
47+
console.log(e2);
4448

4549
// Array.prototype.sort()
4650
// 3. Sort the inventors by birthdate, oldest to youngest
51+
const e3 = inventors.sort((a, b) => a.year - b.year);
52+
console.table(e3);
53+
4754

4855
// Array.prototype.reduce()
4956
// 4. How many years did all the inventors live all together?
57+
const e4 = inventors.reduce((totalYear, inventor) => totalYear + (inventor.passed - inventor.year), 0);
58+
console.log(e4);
5059

5160
// 5. Sort the inventors by years lived
61+
const e5 = inventors.sort((a, b) => (a.passed - a.year - b.passed + b.year));
62+
e5.map(inventor => inventor['lived'] = inventor.passed - inventor.year);
63+
console.table(e5);
5264

5365
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
5466
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
5567

56-
5768
// 7. sort Exercise
5869
// Sort the people alphabetically by last name
70+
const e7 = people.sort((a, b) => {
71+
const [aLast, aFirst] = a.split(', ');
72+
const [bLast, bFirst] = b.split(', ');
73+
return aLast > bLast ? 1 : -1;
74+
});
75+
console.log(e7);
5976

6077
// 8. Reduce Exercise
6178
// Sum up the instances of each of these
6279
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
63-
80+
const e8 = data.reduce((obj, item) => {
81+
if (!obj[item]) {
82+
obj[item] = 0;
83+
}
84+
obj[item]++;
85+
return obj;
86+
}, {});
87+
console.log(e8);
6488
</script>
6589
</body>
6690
</html>

0 commit comments

Comments
 (0)