|
1 | 1 | <!DOCTYPE html>
|
2 | 2 | <html lang="en">
|
3 | 3 | <head>
|
4 |
| - <meta charset="UTF-8"> |
5 |
| - <title>Array Cardio 💪</title> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Array Cardio 💪</title> |
6 | 6 | </head>
|
7 | 7 | <body>
|
8 |
| - <p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
9 |
| - <script> |
10 |
| - // Get your shorts on - this is an array workout! |
11 |
| - // ## Array Cardio Day 1 |
| 8 | + <p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
| 9 | + <script> |
| 10 | + // Get your shorts on - this is an array workout! |
| 11 | + // ## Array Cardio Day 1 |
12 | 12 |
|
13 |
| - // Some data we can work with |
| 13 | + // Some data we can work with |
14 | 14 |
|
15 |
| - const inventors = [ |
| 15 | + const inventors = [ |
16 | 16 | { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
|
17 | 17 | { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
|
18 | 18 | { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
|
|
25 | 25 | { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
|
26 | 26 | { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
|
27 | 27 | { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
|
28 |
| - ]; |
| 28 | + ]; |
29 | 29 |
|
30 |
| - 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']; |
| 30 | + 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']; |
31 | 31 |
|
32 |
| - const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William']; |
| 32 | + const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William']; |
33 | 33 |
|
34 |
| - // Array.prototype.filter() |
35 |
| - // 1. Filter the list of inventors for those who were born in the 1500's |
| 34 | + // Array.prototype.filter() |
| 35 | + // 1. Filter the list of inventors for those who were born in the 1500's |
| 36 | + const bornInSixteenth = inventors.filter(element => |
| 37 | + (element.year >= 1500 && element.year<1600 ) //if it returns true, records are kept |
| 38 | + ); |
36 | 39 |
|
37 |
| - // Array.prototype.map() |
38 |
| - // 2. Give us an array of the inventors' first and last names |
| 40 | + // Array.prototype.map() |
| 41 | + // 2. Give us an array of the inventors' first and last names |
| 42 | + //inny zapis: |
| 43 | + const fullNames = inventors.map(function(element){return `${element.first} ${element.last}`}) |
39 | 44 |
|
40 |
| - // Array.prototype.sort() |
41 |
| - // 3. Sort the inventors by birthdate, oldest to youngest |
| 45 | + // Array.prototype.sort() |
| 46 | + // 3. Sort the inventors by birthdate, oldest to youngest |
42 | 47 |
|
43 |
| - // Array.prototype.reduce() |
44 |
| - // 4. How many years did all the inventors live? |
| 48 | + const ordered = inventors.sort((a,b) => a.year-b.year); |
| 49 | + console.table(ordered); |
| 50 | + // Array.prototype.reduce() |
| 51 | + // 4. How many years did all the inventors live? |
45 | 52 |
|
46 |
| - // 5. Sort the inventors by years lived |
| 53 | + const totalYears = inventors.reduce(function(total, inventor){ |
| 54 | + return total + (inventor.passed - inventor.year); |
| 55 | + },0); |
47 | 56 |
|
48 |
| - // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
49 |
| - // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 57 | + console.log(totalYears); |
50 | 58 |
|
| 59 | + // 5. Sort the inventors by years lived |
51 | 60 |
|
52 |
| - // 7. sort Exercise |
53 |
| - // Sort the people alphabetically by last name |
| 61 | + const sortedLived = inventors.sort((a,b) => { |
| 62 | + const lastAge = a.passed - a.year; |
| 63 | + const nextAge = b.passed - b.year; |
54 | 64 |
|
55 |
| - // 8. Reduce Exercise |
56 |
| - // Sum up the instances of each of these |
57 |
| - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 65 | + return lastAge - nextAge; |
| 66 | + }); |
58 | 67 |
|
59 |
| - </script> |
| 68 | + console.table(sortedLived); |
| 69 | + |
| 70 | + // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 71 | + // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 72 | + // |
| 73 | + // const list = Array.from(document.querySelector('.mw-category').querySelectorAll('a')); |
| 74 | + // const streetNames = list.map(link => link.textContent); |
| 75 | + // console.log(streetNames); |
| 76 | + // const filterStreet = streetNames.filter(element => element.includes('de')); |
| 77 | + |
| 78 | + |
| 79 | + // 7. sort Exercise |
| 80 | + // Sort the people alphabetically by last name |
| 81 | + |
| 82 | + const sortedPeople = people.sort(function(a,b){ |
| 83 | + let [a_lastName, a_firstName] = a.split(', '); |
| 84 | + let [b_lastName, b_firstName] = b.split(', '); |
| 85 | + |
| 86 | + return a_lastName > b_lastName ? 1 : -1; |
| 87 | + |
| 88 | + }); |
| 89 | + console.log(sortedPeople); |
| 90 | + |
| 91 | + // 8. Reduce Exercise |
| 92 | + // Sum up the instances of each of these |
| 93 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 94 | + |
| 95 | + const transportation = data.reduce(function(object, item){ |
| 96 | + |
| 97 | + if (!object[item]) { |
| 98 | + object[item] = 0; |
| 99 | + } |
| 100 | + object[item]++; |
| 101 | + return object; |
| 102 | + },{}); |
| 103 | + console.log(transportation); |
| 104 | + |
| 105 | + </script> |
60 | 106 | </body>
|
61 | 107 | </html>
|
0 commit comments