Skip to content

Commit da0a874

Browse files
committed
Array cardio wesbos#1
1 parent a23f7ec commit da0a874

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,65 @@
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 <= 1599));
42+
console.table(fifteen);
4143

4244
// Array.prototype.map()
4345
// 2. Give us an array of the inventors first and last names
46+
const fullName = inventors.map (inventor=> `${inventor.first} ${inventor.second}`);
47+
console.log (fullName);
4448

4549
// Array.prototype.sort()
4650
// 3. Sort the inventors by birthdate, oldest to youngest
51+
const sortedBirthdate = inventors.sort ((a,b)=> a.year>b.year ? 1: -1);
52+
console.table(sortedBirthdate);
4753

4854
// Array.prototype.reduce()
4955
// 4. How many years did all the inventors live all together?
56+
const livedYears = inventors.reduce ((total,inventor) => {
57+
return total + (inventor.passed - inventor.year);
58+
},0);
59+
console.log(livedYears);
5060

5161
// 5. Sort the inventors by years lived
62+
const oldest = inventors.sort ((a,b)=> {
63+
const lastGuy = a.passed - a.year;
64+
const firstGuy = b.passed - b.year;
65+
return lastGuy > firstGuy ? -1 : 1;
66+
});
67+
console.table(oldest);
68+
5269

5370
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
5471
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
72+
// const category = document.querySelector(".mw-content-ltr");
73+
// const links = category.querySelectorAll('a');
74+
5575

5676

5777
// 7. sort Exercise
5878
// Sort the people alphabetically by last name
5979

80+
const reverseSort = people.sort ((firstName,lastName)=>{
81+
const [aLast, aFirst] = firstName.split(', ');
82+
const [bLast, bFirst] = lastName.split(', ');
83+
return aLast > bLast ? 1:-1});
84+
console.table(reverseSort);
85+
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 vehicles = data.reduce(function(obj,item){
91+
if (!obj[item]){
92+
obj [item]=0;
93+
}
94+
obj[item]++;
95+
return obj;
96+
},{});
97+
98+
console.log(vehicles);
99+
64100
</script>
65101
</body>
66102
</html>

0 commit comments

Comments
 (0)