Skip to content

Commit 6258422

Browse files
committed
Completed exercise wesbos#4
1 parent 077b9bd commit 6258422

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,66 @@
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 => {
42+
if(inventor.year >= 1500 && inventor.year < 1600) {
43+
return true;
44+
}
45+
})
46+
console.table(fifteen)
4147

4248
// Array.prototype.map()
4349
// 2. Give us an array of the inventors first and last names
50+
const names = inventors.map(inventor => inventor.first + " " + inventor.last)
51+
console.log(names)
52+
4453

4554
// Array.prototype.sort()
4655
// 3. Sort the inventors by birthdate, oldest to youngest
56+
const sorted = inventors.sort((a, b) => {
57+
return a.year - b.year
58+
});
59+
console.table(sorted)
4760

4861
// Array.prototype.reduce()
4962
// 4. How many years did all the inventors live all together?
63+
const totalAge = inventors.reduce((total, inventor) => {
64+
return total + (inventor.passed - inventor.year);
65+
}, 0)
66+
console.log(totalAge)
5067

5168
// 5. Sort the inventors by years lived
69+
const sortedAlive = inventors.sort((a, b) => {
70+
const lastGuy = a.passed - a.year;
71+
const nextGuy = b.passed - b.year;
72+
return lastGuy > nextGuy ? -1 : 1
73+
})
74+
console.table(sortedAlive)
5275

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

5679

5780
// 7. sort Exercise
5881
// Sort the people alphabetically by last name
82+
const sortedNames = people.sort()
83+
console.log(sortedNames)
84+
5985

6086
// 8. Reduce Exercise
6187
// Sum up the instances of each of these
6288
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
6389

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+
console.log(transportation)
98+
99+
100+
64101
</script>
65102
</body>
66103
</html>

0 commit comments

Comments
 (0)