Skip to content

Commit 38a049c

Browse files
Array cardio day 1
1 parent 554ff99 commit 38a049c

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,88 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
const result1 = inventors.filter(investor => investor.year >= 1500 && investor.year < 1600);
35+
console.info('Result 1');
36+
console.table(result1)
3437

3538
// Array.prototype.map()
3639
// 2. Give us an array of the inventors first and last names
40+
const result2 = inventors.map(investor => `${investor.first} ${investor.last}`);
41+
console.info('Result 2');
42+
console.table(result2)
3743

3844
// Array.prototype.sort()
3945
// 3. Sort the inventors by birthdate, oldest to youngest
46+
const result3 = inventors.sort((a, b) => {
47+
const inventorABirth = a.year;
48+
const inventorBBirth = b.year;
49+
50+
return inventorBBirth > inventorABirth ? -1 : 1;
51+
})
52+
console.info('Result 3');
53+
console.table(result3);
4054

4155
// Array.prototype.reduce()
4256
// 4. How many years did all the inventors live all together?
57+
const reducer = (accumulator, val) => {
58+
return accumulator + (val.passed - val.year)
59+
}
60+
console.info('Result 4');
61+
console.log(inventors.reduce(reducer, 0));
4362

4463
// 5. Sort the inventors by years lived
64+
const result5 = inventors.sort((a, b) => {
65+
const inventorALived = a.passed - a.year;
66+
const inventorBLived = b.passed - b.year;
67+
68+
return inventorALived > inventorBLived ? 1 : -1;
69+
})
70+
71+
console.info('Result 5');
72+
console.table(result5);
4573

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

4977

5078
// 7. sort Exercise
5179
// Sort the people alphabetically by last name
80+
const result7 = people.sort((a, b) => {
81+
let lastnameA = a.split(', ')[1];
82+
let lastnameB = b.split(', ')[1];
83+
let nameA = lastnameA.toUpperCase(); // ignore upper and lowercase
84+
let nameB = lastnameB.toUpperCase();
85+
86+
if (nameA < nameB) {
87+
return -1;
88+
}
89+
if (nameA > nameB) {
90+
return 1;
91+
}
92+
93+
// names must be equal
94+
return 0;
95+
})
96+
97+
console.info('Result 7');
98+
console.table(result7)
5299

53100
// 8. Reduce Exercise
54101
// Sum up the instances of each of these
55102
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
103+
const carReducer = (accumulator, val) => {
104+
if (!accumulator[val]) {
105+
accumulator[val] = 0;
106+
}
107+
108+
accumulator[val] ++;
109+
110+
return accumulator;
111+
};
112+
113+
const result8 = data.reduce(carReducer, {});
114+
console.info('Result 8');
115+
console.log(result8);
56116

57117
</script>
58118
</body>

0 commit comments

Comments
 (0)