|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
| 3 | + |
3 | 4 | <head> |
4 | | - <meta charset="UTF-8"> |
5 | | - <title>Array Cardio 💪</title> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <title>Array Cardio 💪</title> |
6 | 7 | </head> |
| 8 | + |
7 | 9 | <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 |
| 10 | + <p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
| 11 | + <script> |
| 12 | + // Get your shorts on - this is an array workout! |
| 13 | + // ## Array Cardio Day 1 |
| 14 | + |
| 15 | + // Some data we can work with |
| 16 | + |
| 17 | + const inventors = [{ |
| 18 | + first: 'Albert', |
| 19 | + last: 'Einstein', |
| 20 | + year: 1879, |
| 21 | + passed: 1955 |
| 22 | + }, { |
| 23 | + first: 'Isaac', |
| 24 | + last: 'Newton', |
| 25 | + year: 1643, |
| 26 | + passed: 1727 |
| 27 | + }, { |
| 28 | + first: 'Galileo', |
| 29 | + last: 'Galilei', |
| 30 | + year: 1564, |
| 31 | + passed: 1642 |
| 32 | + }, { |
| 33 | + first: 'Marie', |
| 34 | + last: 'Curie', |
| 35 | + year: 1867, |
| 36 | + passed: 1934 |
| 37 | + }, { |
| 38 | + first: 'Johannes', |
| 39 | + last: 'Kepler', |
| 40 | + year: 1571, |
| 41 | + passed: 1630 |
| 42 | + }, { |
| 43 | + first: 'Nicolaus', |
| 44 | + last: 'Copernicus', |
| 45 | + year: 1473, |
| 46 | + passed: 1543 |
| 47 | + }, { |
| 48 | + first: 'Max', |
| 49 | + last: 'Planck', |
| 50 | + year: 1858, |
| 51 | + passed: 1947 |
| 52 | + }, { |
| 53 | + first: 'Katherine', |
| 54 | + last: 'Blodgett', |
| 55 | + year: 1898, |
| 56 | + passed: 1979 |
| 57 | + }, { |
| 58 | + first: 'Ada', |
| 59 | + last: 'Lovelace', |
| 60 | + year: 1815, |
| 61 | + passed: 1852 |
| 62 | + }, { |
| 63 | + first: 'Sarah E.', |
| 64 | + last: 'Goode', |
| 65 | + year: 1855, |
| 66 | + passed: 1905 |
| 67 | + }, { |
| 68 | + first: 'Lise', |
| 69 | + last: 'Meitner', |
| 70 | + year: 1878, |
| 71 | + passed: 1968 |
| 72 | + }, { |
| 73 | + first: 'Hanna', |
| 74 | + last: 'Hammarström', |
| 75 | + year: 1829, |
| 76 | + passed: 1909 |
| 77 | + }]; |
12 | 78 |
|
13 | | - // Some data we can work with |
| 79 | + const flavours = ['Chocolate Chip', 'Kulfi', 'Caramel Praline', 'Chocolate', 'Burnt Caramel', 'Pistachio', 'Rose', 'Sweet Coconut', 'Lemon Cookie', 'Toffeeness', 'Toasted Almond', 'Black Raspberry Crunch', 'Chocolate Brownies', |
| 80 | + 'Pistachio Almond', 'Strawberry', 'Lavender Honey', 'Lychee', 'Peach', 'Black Walnut', 'Birthday Cake', 'Mexican Chocolate', 'Mocha Almond Fudge', 'Raspberry' |
| 81 | + ]; |
14 | 82 |
|
15 | | - const inventors = [ |
16 | | - { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
17 | | - { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 }, |
18 | | - { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 }, |
19 | | - { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }, |
20 | | - { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 }, |
21 | | - { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 }, |
22 | | - { first: 'Max', last: 'Planck', year: 1858, passed: 1947 }, |
23 | | - { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 }, |
24 | | - { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 }, |
25 | | - { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 }, |
26 | | - { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 }, |
27 | | - { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 } |
28 | | - ]; |
| 83 | + 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', |
| 84 | + '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', |
| 85 | + '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', |
| 86 | + 'Blair, Tony', 'Blake, William' |
| 87 | + ]; |
29 | 88 |
|
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']; |
| 89 | + // Array.prototype.filter() -> Tworzy nową tablicę z wszystkimi elementami, które przechodzą poprawnie test zrealizowany w postaci dostarczonej funkcji. |
| 90 | + // 1. Filter the list of inventors for those who were born in the 1500's: |
| 91 | + const fifteen = inventors.filter(function(inventor) { |
| 92 | + if (inventor.year >= 1500 && inventor.year <= 1599) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + }); |
| 96 | + //lub krócej |
| 97 | + // const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year <= 1599)); |
| 98 | + console.table(fifteen); |
31 | 99 |
|
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 | 100 |
|
34 | | - // Array.prototype.filter() |
35 | | - // 1. Filter the list of inventors for those who were born in the 1500's |
| 101 | + // Array.prototype.map() -> Tworzy nową tablicę z wynikami wywołanej funkcji dla każdego elementu w tablicy. Zawsze zwraca tablicę o tej samej długości. |
| 102 | + // 2. Give us an array of the inventors' first and last names |
| 103 | + const fullNames = inventors.map(function(inventor) { |
| 104 | + return (inventor.first + ' ' + inventor.last); |
| 105 | + }); |
| 106 | + //lub krócej |
| 107 | + // const fullNames = inventors.map(inventor => (`${inventor.first} ${inventor.last}`)); |
| 108 | + console.log(fullNames); |
36 | 109 |
|
37 | | - // Array.prototype.map() |
38 | | - // 2. Give us an array of the inventors' first and last names |
39 | 110 |
|
40 | | - // Array.prototype.sort() |
41 | | - // 3. Sort the inventors by birthdate, oldest to youngest |
| 111 | + // Array.prototype.sort() -> Sortuje elementy tablicy. |
| 112 | + // 3. Sort the inventors by birthdate, oldest to youngest |
| 113 | + const ordered = inventors.sort(function(firstPerson, secondPerson) { |
| 114 | + if (firstPerson.year > secondPerson.year) { |
| 115 | + return 1; |
| 116 | + } else { |
| 117 | + return -1; |
| 118 | + } |
| 119 | + }); |
| 120 | + //lub krócej |
| 121 | + // const ordered = inventors.sort((firstPerson, secondPerson) => firstPerson.year > secondPerson.year ? 1 : -1); |
| 122 | + console.table(ordered); |
42 | 123 |
|
43 | | - // Array.prototype.reduce() |
44 | | - // 4. How many years did all the inventors live? |
| 124 | + // Array.prototype.reduce() -> wywołuje funkcję względem wartości przyrostowej z każdego wywołania i kolejnego elementu tablicy (od lewej do prawej) w celu sprowadzenia tej tablicy do pojedynczej wartości. |
| 125 | + // 4. How many years did all the inventors live? |
| 126 | + const totalYears = inventors.reduce((total, inventor) => { |
| 127 | + return total + (inventor.passed - inventor.year); |
| 128 | + }, 0); // 0 jako zdefiniowanie total |
| 129 | + console.log(totalYears); |
45 | 130 |
|
46 | | - // 5. Sort the inventors by years lived |
| 131 | + // można także pętlą for |
| 132 | + // var totalYears = 0; |
| 133 | + // for (var i = 0; i < inventors.length; i++) { |
| 134 | + // totalYears += inventors[i].year; |
| 135 | + // } |
| 136 | + // console.log(totalYears); |
47 | 137 |
|
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 |
| 138 | + // 5. Sort the inventors by years lived |
| 139 | + const oldest = inventors.sort(function(a, b) { |
| 140 | + const lastGay = a.passed - a.year; |
| 141 | + const nextGay = b.passed - b.year; |
| 142 | + if (lastGay > nextGay) { |
| 143 | + // return -1 |
| 144 | + // } else { |
| 145 | + // return 1 |
| 146 | + //} |
50 | 147 |
|
| 148 | + // lub krócej: |
| 149 | + return (lastGay > nextGay ? -1 : 1) |
| 150 | + } |
| 151 | + }); |
| 152 | + console.table(oldest); |
51 | 153 |
|
52 | | - // 7. sort Exercise |
53 | | - // Sort the people alphabetically by last name |
| 154 | + // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 155 | + // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 156 | + //kod należy odpalic na powyższej stronie w konsoli |
54 | 157 |
|
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' ]; |
| 158 | + // const category = document.querySelector('.mw-category'); |
| 159 | + // const links = Array.from(category.querySelectorAll('a')); |
| 160 | + // const de = links.map(link => link.textContent).filter(streetName => streetName.includes('de')); |
58 | 161 |
|
59 | | - </script> |
| 162 | + // w przypadku .map(link => link.textContent) pojawia się komunikat links.map is not a function dlatego, że zwracana jest NodeList - nie ma dla niej wszystkich funkcji tablicowych np .map() dlatego należy użyć Array.from(); |
| 163 | + |
| 164 | + |
| 165 | + // 7. sort Exercise |
| 166 | + // Sort the people alphabetically by last name |
| 167 | + const alpha = people.sort(function(surname, name) { |
| 168 | + const [aLast, aFirst] = surname.split(', '); |
| 169 | + const [bLast, bFirst] = name.split(', '); |
| 170 | + return aLast > bLast ? 1 : -1; |
| 171 | + }); |
| 172 | + console.log(alpha); |
| 173 | + |
| 174 | + // 8. Reduce Exercise |
| 175 | + // Sum up the instances of each of these |
| 176 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck']; |
| 177 | + const transportation = data.reduce(function(obj, item) { |
| 178 | + if (!obj[item]) { //nie wiemy czy obiekt istnieje więc sprawdzamy i nadajemy mu wartość początkową |
| 179 | + obj[item] = 0; |
| 180 | + } |
| 181 | + obj[item]++; |
| 182 | + return obj; |
| 183 | + }, {}); |
| 184 | + console.log(transportation); |
| 185 | + </script> |
60 | 186 | </body> |
| 187 | + |
61 | 188 | </html> |
0 commit comments