Visitar URL original
Day Four - JavaScript array cardio · wesbos/JavaScript30@af5b28d · GitHub
Skip to content

Commit af5b28d

Browse files
committed
Day Four - JavaScript array cardio
1 parent ccb259b commit af5b28d

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,80 @@
3333

3434
// Array.prototype.filter()
3535
// 1. Filter the list of inventors for those who were born in the 1500's
36-
36+
const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600));
37+
console.table(fifteen);
38+
3739
// Array.prototype.map()
3840
// 2. Give us an array of the inventors' first and last names
41+
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
42+
console.log(fullNames);
3943

4044
// Array.prototype.sort()
4145
// 3. Sort the inventors by birthdate, oldest to youngest
46+
// const ordered = inventors.sort(function(a, b) {
47+
// if(a.year > b.year) {
48+
// return 1;
49+
// } else {
50+
// return -1;
51+
// }
52+
// });
53+
54+
//Shorter version of above - ternary function.
55+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
56+
console.table(ordered);
4257

4358
// Array.prototype.reduce()
4459
// 4. How many years did all the inventors live?
60+
const totalYears = inventors.reduce((total, inventor) => {
61+
return total + (inventor.passed - inventor.year);
62+
}, 0);
63+
64+
console.log(totalYears);
4565

4666
// 5. Sort the inventors by years lived
67+
const oldest = inventors.sort(function(a, b){
68+
const lastGuy = a.passed - a.year;
69+
const nextGuy = b.passed - b.year;
70+
return lastGuy > nextGuy ? -1 : 1;
71+
72+
// if(lastGuy > nextGuy) {
73+
// return -1;
74+
// } else {
75+
// return 1;
76+
// }
77+
78+
});
79+
console.table(oldest);
4780

4881
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4982
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
50-
83+
// const category = document.querySelector('.mw-category');
84+
// const links = Array.from(category.querySelectorAll('a'));
85+
// const de = links
86+
// .map(link => link.textContent)
87+
// .filter(streetName => streetName.includes('de'));
5188

5289
// 7. sort Exercise
5390
// Sort the people alphabetically by last name
91+
const alpha = people.sort(function(lastOne, nextOne){
92+
const [aLast, aFirst] = lastOne.split(', ');
93+
const [bLast, bFirst] = nextOne.split(', ');
94+
return aLast > bLast ? 1 : -1;
95+
});
96+
console.log(alpha);
5497

5598
// 8. Reduce Exercise
5699
// Sum up the instances of each of these
57100
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
101+
const transportation = data.reduce(function(obj, item){
102+
if (!obj[item]) {
103+
obj[item] = 0;
104+
}
105+
obj[item]++;
106+
return obj;
107+
}, {});
108+
109+
console.log(transportation);
58110

59111
</script>
60112
</body>

0 commit comments

Comments
 (0)