|
33 | 33 |
|
34 | 34 | // Array.prototype.filter() |
35 | 35 | // 1. Filter the list of inventors for those who were born in the 1500's |
36 | | - |
| 36 | + const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); |
| 37 | + console.table(fifteen); |
| 38 | + |
37 | 39 | // Array.prototype.map() |
38 | 40 | // 2. Give us an array of the inventors' first and last names |
| 41 | + const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 42 | + console.log(fullNames); |
39 | 43 |
|
40 | 44 | // Array.prototype.sort() |
41 | 45 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 46 | + // const ordered = inventors.sort(function(a, b) { |
| 47 | + // if(a.year > b.year) { |
| 48 | + // return 1; |
| 49 | + // } else { |
| 50 | + // return -1; |
| 51 | + // } |
| 52 | + // }); |
| 53 | + |
| 54 | + //Shorter version of above - ternary function. |
| 55 | + const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
| 56 | + console.table(ordered); |
42 | 57 |
|
43 | 58 | // Array.prototype.reduce() |
44 | 59 | // 4. How many years did all the inventors live? |
| 60 | + const totalYears = inventors.reduce((total, inventor) => { |
| 61 | + return total + (inventor.passed - inventor.year); |
| 62 | + }, 0); |
| 63 | + |
| 64 | + console.log(totalYears); |
45 | 65 |
|
46 | 66 | // 5. Sort the inventors by years lived |
| 67 | + const oldest = inventors.sort(function(a, b){ |
| 68 | + const lastGuy = a.passed - a.year; |
| 69 | + const nextGuy = b.passed - b.year; |
| 70 | + return lastGuy > nextGuy ? -1 : 1; |
| 71 | + |
| 72 | + // if(lastGuy > nextGuy) { |
| 73 | + // return -1; |
| 74 | + // } else { |
| 75 | + // return 1; |
| 76 | + // } |
| 77 | + |
| 78 | + }); |
| 79 | + console.table(oldest); |
47 | 80 |
|
48 | 81 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
49 | 82 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
50 | | - |
| 83 | + // const category = document.querySelector('.mw-category'); |
| 84 | + // const links = Array.from(category.querySelectorAll('a')); |
| 85 | + // const de = links |
| 86 | + // .map(link => link.textContent) |
| 87 | + // .filter(streetName => streetName.includes('de')); |
51 | 88 |
|
52 | 89 | // 7. sort Exercise |
53 | 90 | // Sort the people alphabetically by last name |
| 91 | + const alpha = people.sort(function(lastOne, nextOne){ |
| 92 | + const [aLast, aFirst] = lastOne.split(', '); |
| 93 | + const [bLast, bFirst] = nextOne.split(', '); |
| 94 | + return aLast > bLast ? 1 : -1; |
| 95 | + }); |
| 96 | + console.log(alpha); |
54 | 97 |
|
55 | 98 | // 8. Reduce Exercise |
56 | 99 | // Sum up the instances of each of these |
57 | 100 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
| 101 | + const transportation = data.reduce(function(obj, item){ |
| 102 | + if (!obj[item]) { |
| 103 | + obj[item] = 0; |
| 104 | + } |
| 105 | + obj[item]++; |
| 106 | + return obj; |
| 107 | + }, {}); |
| 108 | + |
| 109 | + console.log(transportation); |
58 | 110 |
|
59 | 111 | </script> |
60 | 112 | </body> |
|
0 commit comments