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