Skip to content

Commit 8641c58

Browse files
committed
Finished the day
1 parent ff9ef1e commit 8641c58

File tree

1 file changed

+87
-6
lines changed

1 file changed

+87
-6
lines changed

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

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,121 @@
3434
const bornInFifteen = inventors.filter(function(person){
3535
return person.year > 1499 && person.year < 1600;
3636
});
37+
const fifteen = inventors.filter(function(inventor){
38+
if (inventor.year >= 1500 && inventor.year < 1600) {
39+
return true;
40+
}
41+
//if you don't return anything other than true it'll just treat it as false.
42+
});
43+
44+
//filter can be simplified by using the =>
45+
const fifteenSimplifying = inventors.filter(inventor => inventor.year > 1499 && inventor.year < 1600);
46+
47+
//console.table(fifteenSimplifying);
48+
//console.table(bornInFifteen);
49+
//console table outputs data to the console in tabular format
50+
51+
52+
3753

3854
// Array.prototype.map()
3955
// 2. Give us an array of the inventors' first and last names
4056
const firstAndLast = inventors.map(function(person){
4157
return person.first + " " + person.last;
4258
});
4359

60+
const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`);
61+
62+
63+
64+
65+
66+
4467
// Array.prototype.sort()
4568
// 3. Sort the inventors by birthdate, oldest to youngest
46-
inventors.sort(function(a, b){return a.year - b.year;});
69+
//inventors.sort(function(a, b){return a.year - b.year;});
70+
71+
//first person is a, second person is b
72+
//const ordered = inventors.sort(function (a,b){
73+
//if (a.year > b.year){
74+
//return 1;
75+
//} else {
76+
//return -1;
77+
//}
78+
//});
79+
80+
//Using a ternarary(sp?) operator
81+
82+
const ordered = inventors.sort((a,b) => a.year > b.year ? 1 : -1);
83+
84+
// console.table(ordered);
4785

4886

4987

88+
89+
5090
// Array.prototype.reduce()
5191
// 4. How many years did all the inventors live?
52-
const totalYears = inventors.reduce(function(total, person){
53-
console.log (person.passed-person.year);
54-
return total+(person.passed-person.year);
55-
});
56-
console.log (totalYears);
92+
const totalYearsCF = inventors.reduce(function(total, inventor){
93+
return total + (inventor.passed - inventor.year);
94+
}, 0);
95+
96+
//console.log(totalYearsCF);
97+
98+
const totalYears = inventors.reduce((total, inventor) => {
99+
return total + (inventor.passed - inventor.year);
100+
}, 0);
101+
102+
//console.log (totalYears);
103+
104+
//ZERO! You can enter another argument for the reduce function, and that effectively initializes the total var with zero. Basically var total = 0;
105+
106+
57107

58108
// 5. Sort the inventors by years lived
59109

110+
const oldest = inventors.sort((a,b) => (a.passed-a.year) < (b.passed-b.year) ? 1 : -1);
111+
//console.table(oldest);
112+
113+
60114
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
61115
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
116+
//const mainWikiPage = document.querySelector(".mw-category");
117+
//const cityNames = document.querySelectorAll(".mw-category-group ul li a");
118+
//You don't need to do descendant selector, in this case just finding any child of class mw-category that's an anchor would do
119+
// const cityNames = Array.from(document.querySelectorAll('a'));
120+
//Array from converts a Node List into an array
121+
122+
//const de = cityNames
123+
// .map(link => link.textContent)
124+
// .filter(streetName => streetName.includes("de") && !(streetName.includes("http")));
125+
62126

63127

64128
// 7. sort Exercise
65129
// Sort the people alphabetically by last name
130+
const alpha = people.sort((lastOne, nextOne) =>{
131+
const [aLast, aFirst] = lastOne.split(', ');
132+
const [bLast, bFirst] = nextOne.split(', ');
133+
return aLast > bLast ? 1 : -1;
134+
});
135+
//console.log(alpha);
66136

67137
// 8. Reduce Exercise
68138
// Sum up the instances of each of these
69139
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
70140

141+
const transportation = data.reduce((obj, item) => {
142+
if (!obj[item]) {
143+
obj[item] = 0;
144+
}
145+
obj[item]++;
146+
return obj;
147+
},{});
148+
149+
console.log(transportation);
150+
151+
71152
</script>
72153
</body>
73154
</html>

0 commit comments

Comments
 (0)