Skip to content

Commit 3492ede

Browse files
committed
Array Cardio finished
1 parent 39534fd commit 3492ede

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,71 @@
3232

3333
// Array.prototype.filter()
3434
// 1. Filter the list of inventors for those who were born in the 1500's
35+
const fifteen = inventors.filter(function(inventor) {
36+
if(inventor.year >= 1500 && inventor.year < 1600) {
37+
return true; // keep it
38+
}
39+
});
40+
console.table(fifteen);
3541

3642
// Array.prototype.map()
3743
// 2. Give us an array of the inventors' first and last names
44+
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`)
45+
console.log(fullNames);
3846

3947
// Array.prototype.sort()
4048
// 3. Sort the inventors by birthdate, oldest to youngest
49+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
50+
//turnurary operator
51+
console.table(ordered);
4152

4253
// Array.prototype.reduce()
4354
// 4. How many years did all the inventors live?
55+
const totalYears = inventors.reduce((total, inventor)=> {
56+
return total + (inventor.passed - inventor.year);
57+
},0);
58+
console.log(totalYears);
4459

4560
// 5. Sort the inventors by years lived
61+
const oldest = inventors.sort(function(a, b) {
62+
const lastGuy = a.passed - a.year;
63+
const nextGuy = b.passed - b.year;
64+
return lastGuy > nextGuy ? -1 : 1;
65+
});
66+
console.table(oldest);
4667

4768
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4869
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
70+
// const category = document.querySelector('.mw-category');
71+
// const links = [...category.querySelectorAll('a')];
72+
// const de = links
73+
// .map(link => link.textContent)
74+
// .filter(streetName => streetName.includes('de'));
4975

5076

5177
// 7. sort Exercise
5278
// Sort the people alphabetically by last name
79+
const alpha = people.sort(function(total, nextOne) {
80+
const[aLast, aFirst] = nextOne.split(', ');
81+
const[bLast, bFirst] = nextOne.split(', ');
82+
return aLast > bLast ? 1 : -1;
83+
});
84+
console.log(alpha);
5385

5486
// 8. Reduce Exercise
5587
// Sum up the instances of each of these
5688
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5789

90+
const transportation = data.reduce(function(obj, item) {
91+
if(!obj[item]) {
92+
obj[item] = 0;
93+
}
94+
obj[item]++;
95+
return obj;
96+
}, {});
97+
98+
console.log(transportation);
99+
58100
</script>
59101
</body>
60102
</html>

0 commit comments

Comments
 (0)