|
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 | + const fifteen = inventors.filter(inventor => |
| 37 | + inventor.year >= 1500 && inventor.year < 1600) |
| 38 | + console.table(fifteen); |
36 | 39 |
|
37 | 40 | // Array.prototype.map() |
38 | 41 | // 2. Give us an array of the inventors' first and last names |
| 42 | + //will always return the same number of items as you give it |
| 43 | + // const fullNames = inventors.map(inventor => |
| 44 | + // inventor.first + ' ' + inventor.last); |
| 45 | + // console.log(fullNames); |
| 46 | + // //or |
| 47 | + const fullNames = inventors.map(inventor => |
| 48 | + `${inventor.first} ${inventor.last}`); |
| 49 | + console.log(fullNames); |
39 | 50 |
|
40 | 51 | // Array.prototype.sort() |
41 | 52 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 53 | + //you get two items and you have to sort those two |
| 54 | + |
| 55 | + // const ordered = inventors.sort(function(a, b){ |
| 56 | + // if (a.year > b.year){ |
| 57 | + // return 1; |
| 58 | + // } else { |
| 59 | + // return -1; |
| 60 | + // } |
| 61 | + // }) |
| 62 | + |
| 63 | + // console.table(ordered) |
| 64 | + //or |
| 65 | + |
| 66 | + const ordered = inventors.sort((a, b) => |
| 67 | + a.year > b.year ? 1 : -1); |
| 68 | + |
| 69 | + console.table(ordered); |
42 | 70 |
|
43 | 71 | // Array.prototype.reduce() |
44 | 72 | // 4. How many years did all the inventors live? |
| 73 | + const totalYears = inventors.reduce((total, inventor) => { |
| 74 | + return total + (inventor.passed - inventor.year); |
| 75 | + //to get the number of years they lived |
| 76 | + }, 0) |
| 77 | + console.log(totalYears); |
45 | 78 |
|
46 | 79 | // 5. Sort the inventors by years lived |
| 80 | + const oldest = inventors.sort(function(a, b){ |
| 81 | + const lastGuy = a.passed - a.year; |
| 82 | + const nextGuy = b.passed - b.year; |
| 83 | + return lastGuy > nextGuy ? -1 : 1; |
| 84 | + // if(lastGuy > nextGuy){ |
| 85 | + // return -1; |
| 86 | + // } else { |
| 87 | + // return 1; |
| 88 | + // } |
| 89 | + }) |
| 90 | + console.table(oldest); |
47 | 91 |
|
48 | 92 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
49 | 93 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
50 | 94 |
|
| 95 | + //IN CONSOLE OF WIKI PAGE^ |
| 96 | + //dev tools |
| 97 | + // |
| 98 | +// const category = document.querySelector('.mw-category'); |
| 99 | +// const links = Array.from(category.querySelectorAll('a')); |
| 100 | +// //convert list of links to list of names |
| 101 | +// //array.from will make it into an array from the nodelist |
| 102 | + |
| 103 | +// const de = links |
| 104 | +// .map(link => link.textContent) |
| 105 | +// //will give the text inside of each link |
| 106 | +// .filter(streetName => streetName.includes('de')); |
| 107 | +// //will filter them out |
51 | 108 |
|
52 | 109 | // 7. sort Exercise |
53 | 110 | // Sort the people alphabetically by last name |
| 111 | + const alpha = people.sort((lastOne, nextOne) => { |
| 112 | + // console.log(lastOne); |
| 113 | + const [last, first] = lastOne.split(', '); |
| 114 | + const [lastN, firstN] = nextOne.split(', '); |
| 115 | + // console.log(last, first); |
| 116 | + // console.log(lastN, firstN); |
| 117 | + return last > lastN ? 1 : -1; |
| 118 | + }) |
| 119 | + console.log(alpha); |
54 | 120 |
|
55 | 121 | // 8. Reduce Exercise |
56 | 122 | // Sum up the instances of each of these |
57 | 123 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
58 | 124 |
|
| 125 | + const transportation = data.reduce(function(obj, item) { |
| 126 | + if (!obj[item]){ |
| 127 | + obj[item] = 0; |
| 128 | + } |
| 129 | + |
| 130 | + // console.log(item); |
| 131 | + obj[item] ++; |
| 132 | + return obj; |
| 133 | + }, {}) |
| 134 | + console.log(transportation); |
| 135 | + |
59 | 136 | </script> |
60 | 137 | </body> |
61 | 138 | </html> |
0 commit comments