Skip to content

Commit 9e679d0

Browse files
committed
finished JS Day wesbos#4
1 parent 1e976da commit 9e679d0

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
@@ -31,15 +31,31 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
const filteredInventors = inventors.filter(inventor => inventor.year < 1600 && inventor.year >= 1500 )
35+
3436

3537
// Array.prototype.map()
3638
// 2. Give us an array of the inventors' first and last names
39+
const names = inventors.map(inventor => {
40+
return `${inventor.first} ${inventor.last}`
41+
})
3742

3843
// Array.prototype.sort()
3944
// 3. Sort the inventors by birthdate, oldest to youngest
45+
const sortByBirth = inventors.sort((x,y) => {
46+
const ageX = x.passed - x.year
47+
const ageY = y.passed - y.year
48+
return ageX < ageY ? 1 : -1
49+
50+
})
51+
4052

4153
// Array.prototype.reduce()
4254
// 4. How many years did all the inventors live?
55+
const totalAge = inventors.reduce((total, inventor) => {
56+
const age = inventor.passed - inventor.year
57+
return total + age
58+
}, 0)
4359

4460
// 5. Sort the inventors by years lived
4561

@@ -49,11 +65,31 @@
4965

5066
// 7. sort Exercise
5167
// Sort the people alphabetically by last name
68+
const sortPeople = people.sort(function(firstOne, lastOne){
69+
const [aFirst, aLast] = firstOne.split(',');
70+
const [bFirst, bLast] = lastOne.split(',');
71+
return aLast > bLast ? 1 : -1
72+
})
73+
74+
console.log(sortPeople)
75+
76+
5277

5378
// 8. Reduce Exercise
5479
// Sum up the instances of each of these
5580
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5681

82+
const sum = data.reduce((total, single) => {
83+
if(!total[single]){
84+
total[single] = 1
85+
}else{
86+
total[single] += 1
87+
}
88+
return total
89+
}, {})
90+
91+
92+
5793
</script>
5894
</body>
5995
</html>

0 commit comments

Comments
 (0)