Skip to content

Commit 83006cd

Browse files
committed
Day wesbos#4 array cardio 1
1 parent 6003c9b commit 83006cd

File tree

2 files changed

+40
-109
lines changed

2 files changed

+40
-109
lines changed

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

Lines changed: 0 additions & 106 deletions
This file was deleted.

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Array Cardio 💪</title>
67
</head>
8+
79
<body>
8-
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
10+
<p>
11+
<em>Psst: have a look at the JavaScript Console</em> 💁</p>
912
<script>
1013
// Get your shorts on - this is an array workout!
1114
// ## Array Cardio Day 1
@@ -31,29 +34,63 @@
3134

3235
// Array.prototype.filter()
3336
// 1. Filter the list of inventors for those who were born in the 1500's
37+
const filteredInventors = inventors.filter(inventor => inventor.year < 1600 && inventor.year >= 1500);
38+
console.log('filtered inventors', filteredInventors);
3439

3540
// Array.prototype.map()
3641
// 2. Give us an array of the inventors' first and last names
42+
const mappedInventors = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
43+
console.log('mapped inventors', mappedInventors);
3744

3845
// Array.prototype.sort()
3946
// 3. Sort the inventors by birthdate, oldest to youngest
47+
const sortedInventors = inventors.sort((a, b) => (a.year > b.year) ? 1 : -1);
48+
console.log('sorted inventors', sortedInventors);
4049

4150
// Array.prototype.reduce()
4251
// 4. How many years did all the inventors live?
52+
const reducedInventors = inventors.reduce((total, inventor) => total + inventor.passed - inventor.year, 0);
53+
console.log('reduced inventors', reducedInventors);
54+
4355

4456
// 5. Sort the inventors by years lived
57+
const sortedInventorsByAge = inventors
58+
.sort((a, b) => a.passed - a.year - b.passed + b.year)
59+
.map(item => {
60+
let newItem = item;
61+
newItem.age = item.passed - item.year;
62+
return newItem;
63+
});
64+
console.log('sorted inventors by age', sortedInventorsByAge);
65+
4566

4667
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4768
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
69+
// Array.from(document.querySelectorAll('.mw-category a'))
70+
// .map(item => item.innerText)
71+
// .filter(item => item.match('de'));
4872

4973

5074
// 7. sort Exercise
5175
// Sort the people alphabetically by last name
76+
const sortedPeople = people.sort((a, b) => a.split(', ')[1] > b.split(', ')[1] ? 1 : -1);
77+
console.log('sorted people', sortedPeople);
78+
5279

5380
// 8. Reduce Exercise
5481
// Sum up the instances of each of these
55-
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
82+
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck'];
83+
84+
const transportation = data.reduce((result, item) => {
85+
if (!result[item]) {
86+
result[item] = 0;
87+
}
88+
result[item]++;
89+
return result;
90+
}, {});
91+
console.log('transportation', transportation);
5692

5793
</script>
5894
</body>
59-
</html>
95+
96+
</html>

0 commit comments

Comments
 (0)