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