Skip to content

Commit f56173d

Browse files
committed
Completed lesson 04
1 parent e5e0bea commit f56173d

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,77 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
let sixteenthCentury = inventors.filter(i => i.year > 1499 && i.year < 1600);
35+
// console.table(sixteenthCentury);
3436

3537
// Array.prototype.map()
3638
// 2. Give us an array of the inventors' first and last names
39+
let firstLast = inventors.map(i => `${i.first} ${i.last}`);
40+
// console.log(firstLast);
3741

3842
// Array.prototype.sort()
3943
// 3. Sort the inventors by birthdate, oldest to youngest
44+
inventors.sort((a, b) => b.year - a.year);
45+
// console.log('Oldest to youngest');
46+
// console.table(inventors);
4047

4148
// Array.prototype.reduce()
4249
// 4. How many years did all the inventors live?
50+
let totalYears =inventors.reduce((total, i) => (total + (i.passed - i.year)), 0);
51+
// console.log('Total years', totalYears);
52+
53+
// Wes' solution
54+
// const totalYears = inventors.reduce((total, inventor) => {
55+
// return total + (inventor.passed - inventor.year);
56+
// }, 0);
57+
// console.log('Total years:', totalYears);
4358

4459
// 5. Sort the inventors by years lived
60+
console.log('Sort by years lived');
61+
inventors.forEach(i => i.age = (i.passed - i.year));
62+
inventors.sort((a, b) => b.age - a.age);
63+
console.table(inventors);
4564

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

68+
// Have to run this code against the Wikipedia page
69+
// let category = document.querySelector('.mw-category');
70+
71+
// // querySelectorAll returns a NodeList, not an array
72+
// let links = Array.from(category.querySelectorAll('a'));
73+
74+
// let de = links
75+
// .map(link => link.textContent)
76+
// .filter(link => link.includes(' de '));
4977

5078
// 7. sort Exercise
5179
// Sort the people alphabetically by last name
80+
people.sort((lastOne, nextOne) => {
81+
let [aLast, aFirst] = lastOne.split(', ');
82+
let [bLast, bFirst] = nextOne.split(', ');
83+
84+
if(aLast < bLast) return -1;
85+
else if (aLast === bLast) return 0;
86+
else return 1;
87+
});
88+
// console.log(people);
5289

5390
// 8. Reduce Exercise
5491
// Sum up the instances of each of these
5592
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5693

94+
let transportation = data.reduce((obj, item) => {
95+
if(!obj[item]) {
96+
obj[item] = 0;
97+
}
98+
99+
obj[item]++
100+
return obj;
101+
}, {});
102+
103+
console.table(transportation);
104+
57105
</script>
58106
</body>
59107
</html>

0 commit comments

Comments
 (0)