Skip to content

Commit 6a7c1de

Browse files
committed
add solution
1 parent 6b98a14 commit 6a7c1de

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,73 @@
3838

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

4245
// Array.prototype.map()
4346
// 2. Give us an array of the inventors first and last names
4447

48+
const fullNames = inventors.map( (inventor) => `${inventor.first} ${inventor.last}`);
49+
console.log(fullNames);
50+
4551
// Array.prototype.sort()
4652
// 3. Sort the inventors by birthdate, oldest to youngest
4753

54+
const oldestFirst = inventors.sort( (a,b) => a.year < b.year ? 1 : -1);
55+
console.table(oldestFirst);
56+
4857
// Array.prototype.reduce()
4958
// 4. How many years did all the inventors live all together?
5059

60+
const totalYears = inventors.reduce( (total, inventor) => {
61+
return total + (inventor.passed - inventor.year);
62+
}, 0);
63+
console.log(totalYears);
64+
5165
// 5. Sort the inventors by years lived
66+
const oldest = inventors.sort( (a,b) => {
67+
let first = a.passed - a.year;
68+
let second = b.passed - b.year;
69+
return first < second ? 1 : -1;
70+
});
71+
console.table(oldest);
5272

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

76+
// const category = document.querySelector(".mw-category");
77+
// const links = [...category.querySelectorAll('a')];
78+
79+
// const de = links
80+
// .map(link => link.textContent)
81+
// .filter(streetName => streetName.includes('de'));
82+
83+
5684

5785
// 7. sort Exercise
5886
// Sort the people alphabetically by last name
5987

88+
const sortedPeople = people.sort(function(lastOne, nextOne) {
89+
const [aLast, aFirst] = lastOne.split(', ');
90+
const [bLast, bFirst] = nextOne.split(', ');
91+
return aLast > bLast ? 1 : -1;
92+
})
93+
console.log(sortedPeople);
94+
95+
6096
// 8. Reduce Exercise
6197
// Sum up the instances of each of these
6298
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
99+
100+
const transportation = data.reduce( (obj, item) => {
101+
if (!(item in obj)) obj[item] = 0;
102+
obj[item]++;
103+
return obj;
104+
}, {});
105+
106+
console.log(transportation);
107+
63108

64109
</script>
65110
</body>

07 - Array Cardio Day 2/index-START.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,34 @@
2727

2828
// Some and Every Checks
2929
// Array.prototype.some() // is at least one person 19 or older?
30+
31+
32+
const isAdult = people.some( (person) => {
33+
const currentYear = (new Date()).getFullYear();
34+
if ( currentYear - person.year >= 19) return true;
35+
});
36+
37+
console.log(isAdult);
38+
3039
// Array.prototype.every() // is everyone 19 or older?
3140

41+
const allAdult = people.every( person => ((new Date()).getFullYear()) - person.year >= 19);
42+
console.log(allAdult);
43+
3244
// Array.prototype.find()
3345
// Find is like filter, but instead returns just the one you are looking for
3446
// find the comment with the ID of 823423
3547

48+
const comment = comments.find( (comment) => comment.id === 823423)
49+
console.log(comment);
50+
3651
// Array.prototype.findIndex()
3752
// Find the comment with this ID
3853
// delete the comment with the ID of 823423
3954

55+
const deleted = comments.findIndex( comment => comment.id === 823423);
56+
comments.splice(deleted, 1);
57+
4058
</script>
4159
</body>
4260
</html>

0 commit comments

Comments
 (0)