Skip to content

Commit c36a0f5

Browse files
author
Greg Pfaff
committed
adding working sort solution with a proper check
1 parent 21bea20 commit c36a0f5

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
4+
<meta content="array cardio" charset="UTF-8">
55
<title>Array Cardio 💪</title>
66
</head>
77
<body>
@@ -12,13 +12,13 @@
1212
// Some data we can work with
1313

1414
const inventors = [
15-
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
16-
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
17-
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
18-
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
19-
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
20-
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
21-
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
15+
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, // 76
16+
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, // 84
17+
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, // 78
18+
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, // 67
19+
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, // 59
20+
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }, // 70
21+
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 }, // 89
2222
];
2323

2424
const flavours = ['Chocolate Chip', 'Kulfi', 'Caramel Praline', 'Chocolate', 'Burnt Caramel', 'Pistachio', 'Rose', 'Sweet Coconut', 'Lemon Cookie', 'Toffeeness', 'Toasted Almond', 'Black Raspberry Crunch', 'Chocolate Brownies', 'Pistachio Almond', 'Strawberry', 'Lavender Honey', 'Lychee', 'Peach', 'Black Walnut', 'Birthday Cake', 'Mexican Chocolate', 'Mocha Almond Fudge', 'Raspberry'];
@@ -32,23 +32,32 @@
3232
if (inventor.year >= 1500 && inventor.year <= 1599) {
3333
return true;
3434
}
35-
})
36-
console.table(fifteen)
35+
});
36+
console.table(fifteen);
3737

3838
// Array.prototype.map()
3939
// 2. Give us an array of the inventory first and last names
4040

4141
const fullNames = inventors.map((inventors) => {
42-
4342
return `${inventors.first} ${inventors.last}`;
44-
45-
})
46-
console.log(fullNames);
47-
43+
});
44+
console.table(fullNames);
4845

4946
// Array.prototype.sort()
5047
// 3. Sort the inventors by birthdate, oldest to youngest
5148

49+
const sortedInvetors = inventors.sort((firstPerson, lastPerson) => {
50+
const birthAge = firstPerson.passed - firstPerson.year;
51+
const secondAge = lastPerson.passed - lastPerson.year;
52+
53+
if (birthAge > secondAge) {
54+
return 1;
55+
} else {
56+
return -1;
57+
}
58+
});
59+
console.table(sortedInvetors);
60+
5261
// Array.prototype.reduce()
5362
// 4. How many years did all the inventors live?
5463

0 commit comments

Comments
 (0)