|
1 | | -<!DOCTYPE html> |
2 | | -<html lang="en"> |
3 | | -<head> |
4 | | - <meta charset="UTF-8"> |
5 | | - <title>Array Cardio 💪</title> |
6 | | -</head> |
7 | | -<body> |
8 | | - <p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
9 | | - <script> |
10 | 1 | // Get your shorts on - this is an array workout! |
11 | 2 | // ## Array Cardio Day 1 |
12 | 3 |
|
|
31 | 22 |
|
32 | 23 | // Array.prototype.filter() |
33 | 24 | // 1. Filter the list of inventors for those who were born in the 1500's |
34 | | - const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); |
35 | 25 |
|
36 | | - console.table(fifteen); |
| 26 | + const fifteenHundreds = inventors.filter(function (inventor) { |
| 27 | + if (inventor.year >= 1500 && inventor.year < 1600) { |
| 28 | + return true; // By returning true it means we keep it. |
| 29 | + }; |
| 30 | + }); |
| 31 | + |
| 32 | + // This example also works |
| 33 | + // const fifteenHundreds = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); |
| 34 | + |
| 35 | + console.log(fifteenHundreds); |
37 | 36 |
|
38 | 37 | // Array.prototype.map() |
39 | | - // 2. Give us an array of the inventor first and last names |
40 | | - const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
41 | | - console.log(fullNames); |
| 38 | + // 2. Give us an array of the inventors' first and last names |
| 39 | + |
| 40 | + // My solution. |
| 41 | + const fullName = inventors.map(function (inventor) { |
| 42 | + return inventor.first + ' ' + inventor.last; |
| 43 | + }); |
| 44 | + |
| 45 | + //His solution using concatinating stings (ES6) |
| 46 | + // const fullName = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 47 | + |
| 48 | + console.log(fullName); |
42 | 49 |
|
43 | 50 | // Array.prototype.sort() |
44 | 51 | // 3. Sort the inventors by birthdate, oldest to youngest |
45 | | - // const ordered = inventors.sort(function(a, b) { |
46 | | - // if(a.year > b.year) { |
47 | | - // return 1; |
48 | | - // } else { |
49 | | - // return -1; |
50 | | - // } |
51 | | - // }); |
52 | 52 |
|
53 | | - const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
54 | | - console.table(ordered); |
| 53 | + // My Solution |
| 54 | + const oldestToYoungest = inventors.sort(function (a ,b) { |
| 55 | + return a.year - b.year; |
| 56 | + }); |
| 57 | + |
| 58 | + // His solution |
| 59 | + // const oldestToYoungest = inventors.sort(function (a ,b) { |
| 60 | + // if (a.year > b.year) { |
| 61 | + // |
| 62 | + // return 1; |
| 63 | + // |
| 64 | + // }else{ |
| 65 | + // |
| 66 | + // return -1; |
| 67 | + // } |
| 68 | + // }); |
| 69 | + |
| 70 | + console.log(oldestToYoungest); |
| 71 | + |
55 | 72 |
|
56 | 73 | // Array.prototype.reduce() |
57 | 74 | // 4. How many years did all the inventors live? |
58 | | - const totalYears = inventors.reduce((total, inventor) => { |
59 | | - return total + (inventor.passed - inventor.year); |
60 | | - }, 0); |
61 | 75 |
|
62 | | - console.log(totalYears); |
| 76 | + // His Solution. Not to sure how this one worked |
| 77 | + const totalYears = inventors.reduce((total, inventor) => { |
| 78 | + return total + (inventor.passed - inventor.year) |
| 79 | + }, 0); |
| 80 | + |
| 81 | + console.log('Total Years: ' + totalYears); |
63 | 82 |
|
64 | 83 | // 5. Sort the inventors by years lived |
65 | | - const oldest = inventors.sort(function(a, b) { |
66 | | - const lastInventor = a.passed - a.year; |
67 | | - const nextInventor = b.passed - b.year; |
68 | | - return lastInventor > nextInventor ? -1 : 1; |
| 84 | + |
| 85 | + //His solution |
| 86 | + const yearsLived = inventors.sort(function (a ,b) { |
| 87 | + const firstGuy = a.passed - a.year; |
| 88 | + const nextGuy = b.passed - b.year; |
| 89 | + return firstGuy > nextGuy ? -1 : 1; |
69 | 90 | }); |
70 | | - console.table(oldest); |
| 91 | + |
| 92 | + console.log(yearsLived); |
71 | 93 |
|
72 | 94 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
73 | 95 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
74 | 96 |
|
75 | | - // const category = document.querySelector('.mw-category'); |
76 | | - // const links = Array.from(category.querySelectorAll('a')); |
77 | | - // const de = links |
78 | | - // .map(link => link.textContent) |
79 | | - // .filter(streetName => streetName.includes('de')); |
| 97 | + // His solution |
| 98 | + // First you must look at the HTML doc (dev tools) and find what you need to select. |
| 99 | + // const category = document.querySelector('.mw-category'); |
| 100 | + |
| 101 | + // You can call querySelector against any existing DOM element. |
| 102 | + // You also need to turn the links variable into an array so .map is accessable in devtools. |
| 103 | + // const links = Array.from(category.querySelectorAll('a')); |
| 104 | + // |
| 105 | + // const de = links |
| 106 | + // .map(link => link.textContent) |
| 107 | + // .filter(streetName => streetName.includes('de')); |
80 | 108 |
|
81 | 109 | // 7. sort Exercise |
82 | 110 | // Sort the people alphabetically by last name |
83 | | - const alpha = people.sort((lastOne, nextOne) => { |
84 | | - const [aLast, aFirst] = lastOne.split(', '); |
85 | | - const [bLast, bFirst] = nextOne.split(', '); |
86 | | - return aLast > bLast ? 1 : -1; |
87 | | - }); |
88 | | - console.log(alpha); |
| 111 | + |
| 112 | + // Currently not working. |
| 113 | + // const alpha = people.sort(function (lastOne, nextOne) { |
| 114 | + // const [ aLast, aFirst] = lastOne.split(', '); |
| 115 | + // const [ bLast, bFirst] = lastOne.split(', '); |
| 116 | + // return aLast > bLast ? 1 : -1; |
| 117 | + // }); |
| 118 | + // |
| 119 | + // console.log(alpha); |
89 | 120 |
|
90 | 121 | // 8. Reduce Exercise |
91 | 122 | // Sum up the instances of each of these |
92 | | - const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogostick']; |
| 123 | + const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 124 | + |
93 | 125 |
|
94 | 126 | const transportation = data.reduce(function(obj, item) { |
95 | 127 | if (!obj[item]) { |
|
100 | 132 | }, {}); |
101 | 133 |
|
102 | 134 | console.log(transportation); |
103 | | - |
104 | | - </script> |
105 | | -</body> |
106 | | -</html> |
|
0 commit comments