|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | 3 | <head> |
4 | | - <meta charset="UTF-8"> |
| 4 | + <meta content="array cardio" charset="UTF-8"> |
5 | 5 | <title>Array Cardio 💪</title> |
6 | 6 | </head> |
7 | 7 | <body> |
|
12 | 12 | // Some data we can work with |
13 | 13 |
|
14 | 14 | 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 |
22 | 22 | ]; |
23 | 23 |
|
24 | 24 | 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 | 32 | if (inventor.year >= 1500 && inventor.year <= 1599) { |
33 | 33 | return true; |
34 | 34 | } |
35 | | - }) |
36 | | - console.table(fifteen) |
| 35 | + }); |
| 36 | + console.table(fifteen); |
37 | 37 |
|
38 | 38 | // Array.prototype.map() |
39 | 39 | // 2. Give us an array of the inventory first and last names |
40 | 40 |
|
41 | 41 | const fullNames = inventors.map((inventors) => { |
42 | | - |
43 | 42 | return `${inventors.first} ${inventors.last}`; |
44 | | - |
45 | | - }) |
46 | | - console.log(fullNames); |
47 | | - |
| 43 | + }); |
| 44 | + console.table(fullNames); |
48 | 45 |
|
49 | 46 | // Array.prototype.sort() |
50 | 47 | // 3. Sort the inventors by birthdate, oldest to youngest |
51 | 48 |
|
| 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 | + |
52 | 61 | // Array.prototype.reduce() |
53 | 62 | // 4. How many years did all the inventors live? |
54 | 63 |
|
|
0 commit comments