Skip to content

Commit 5b101e1

Browse files
author
Pete Cowles
committed
finished wesbos#4 - array work
1 parent e5bfe78 commit 5b101e1

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,60 @@
3434
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
3535
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
3636
];
37-
37+
3838
// Array.prototype.filter()
3939
// 1. Filter the list of inventors for those who were born in the 1500's
40+
const fifteens = inventors.filter(person => (person.year >= 1500 && person.year <1600));
41+
console.table(fifteens);
4042

4143
// Array.prototype.map()
4244
// 2. Give us an array of the inventors first and last names
45+
const names = inventors.map(person => person.first + ' ' + person.last);
46+
console.log(names);
4347

4448
// Array.prototype.sort()
4549
// 3. Sort the inventors by birthdate, oldest to youngest
50+
const sorted = inventors.sort((a, b) => a.year - b.year);
51+
console.table(sorted);
4652

4753
// Array.prototype.reduce()
4854
// 4. How many years did all the inventors live all together?
55+
const totalYears = inventors.reduce((total, person) => total += (person.passed - person.year), 0);
56+
console.log(totalYears);
4957

5058
// 5. Sort the inventors by years lived
59+
const youngToOld = inventors.sort((a, b) => ((a.passed - a.year) - (b.passed - b.year)));
60+
console.table(youngToOld);
5161

5262
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
5363
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
64+
// do this one on the web page
65+
66+
// const category = document.querySelector('.mw-catagory');
67+
// const links = Array.from(category.querySelectorAll('a'));
68+
// const de = links.map(link => link.textContent)
69+
// .filter(streetName => streetName.includes('de'));
5470

5571

5672
// 7. sort Exercise
5773
// Sort the people alphabetically by last name
74+
const peopleSorted = people.sort((a, b) => {
75+
const aName = a.split(', ');
76+
const bName = b.split(', ');
77+
return (aName[0] > bName[0]) ? 1 : -1;
78+
});
79+
console.log(peopleSorted);
5880

5981
// 8. Reduce Exercise
6082
// Sum up the instances of each of these
6183
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
6284

85+
const instances = data.reduce((obj, item) => {
86+
(obj[item]) ? obj[item]++ : obj[item] = 1;
87+
return obj
88+
}, {});
89+
console.log(instances);
90+
6391
</script>
6492
</body>
6593
</html>

0 commit comments

Comments
 (0)