Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 564da2b

Browse files
committed
4th exercise done
1 parent 29cae48 commit 564da2b

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,60 @@
3333

3434
// Array.prototype.filter()
3535
// 1. Filter the list of inventors for those who were born in the 1500's
36+
const fifteens = inventors.filter(inventor => inventor.year >= 1500 && inventor.year < 1600);
37+
console.log('Born in 1500s');
38+
console.table(fifteens);
3639

3740
// Array.prototype.map()
3841
// 2. Give us an array of the inventors' first and last names
42+
const names = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
43+
console.log(names);
3944

4045
// Array.prototype.sort()
4146
// 3. Sort the inventors by birthdate, oldest to youngest
47+
const sorts = inventors.sort((a,b) => a.year - b.year);
48+
console.log('Oldest to youngest');
49+
console.table(sorts);
4250

4351
// Array.prototype.reduce()
4452
// 4. How many years did all the inventors live?
53+
const years = inventors.reduce((agg, inv) => {
54+
return agg + (inv.passed - inv.year);
55+
}, 0)
56+
console.log(`Years lived all together: ${years}`);
4557

4658
// 5. Sort the inventors by years lived
59+
const ages = inventors.sort((a,b) => {
60+
return (a.passed - a.year) - (b.passed - b.year);
61+
});
62+
console.log('Sorted by number of years lived');
63+
console.table(ages);
4764

4865
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4966
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
50-
67+
const links = document.querySelectorAll('.mw-category a');
68+
Array.from(links)
69+
.map(link => link.text)
70+
.filter(text => text.includes('de'));
5171

5272
// 7. sort Exercise
5373
// Sort the people alphabetically by last name
74+
const sortedLast = people.sort((a,b) => {
75+
return a.split(', ')[0] < b.split(', ')[0] ? -1 : 1
76+
})
77+
console.log(sortedLast);
5478

5579
// 8. Reduce Exercise
5680
// Sum up the instances of each of these
5781
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
58-
82+
const sums = data.reduce((obj, trans) => {
83+
if (!obj[trans]) {
84+
obj[trans] = 0;
85+
}
86+
obj[trans]++;
87+
return obj;
88+
}, {});
89+
console.log(sums);
5990
</script>
6091
</body>
6192
</html>

0 commit comments

Comments
 (0)