Skip to content

Commit a272e51

Browse files
authored
Merge pull request #1 from Laryl14/ArrayCardio1
Array cardio1
2 parents 01c559f + e51ecfb commit a272e51

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

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

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

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

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

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

4147
// Array.prototype.reduce()
4248
// 4. How many years did all the inventors live all together?
49+
const year = inventors.reduce((totalYears,x) => {
50+
return totalYears + (x.passed - x.year);
51+
}, 0);
52+
console.log(year);
4353

4454
// 5. Sort the inventors by years lived
55+
const sortedYearsLived = inventors.sort(yearsLived);
56+
57+
function yearsLived(a,b) {
58+
return ((b.passed-b.year)-(a.passed-a.year));
59+
}
60+
61+
console.log(sortedYearsLived);
4562

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

4966

5067
// 7. sort Exercise
5168
// Sort the people alphabetically by last name
69+
const sortedLastName = people.sort(sortByLastName);
70+
71+
function sortByLastName(a,b) {
72+
return (getLastName(a)>getLastName(b));
73+
}
74+
75+
function getLastName(nameValue) {
76+
let fullName = nameValue.split(", ");
77+
return fullName[0];
78+
}
79+
80+
console.log(sortedLastName);
5281

5382
// 8. Reduce Exercise
5483
// Sum up the instances of each of these
5584
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5685

86+
const dataObjectArray=[]
87+
for(let i = 0; i<data.length; i++) {
88+
for (let j = 0; j < dataObjectArray.length; j++) {
89+
if (dataObjectArray[j].type == data[i]) {
90+
dataObjectArray[j].amount += 1;
91+
}
92+
}
93+
if(!(dataObjectArray.some(vehicle => vehicle.type == data[i]))) {
94+
let newVehicle = new Object();
95+
newVehicle.type = data[i];
96+
newVehicle.amount = 1;
97+
dataObjectArray.push(newVehicle);
98+
}
99+
}
100+
console.log(dataObjectArray);
101+
102+
103+
57104
</script>
58105
</body>
59106
</html>

0 commit comments

Comments
 (0)