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