|
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 born = inventors.filter(x => x.year >=1500 && x.year < 1600); |
| 35 | + console.log(born); |
34 | 36 |
|
35 | 37 | // Array.prototype.map()
|
36 | 38 | // 2. Give us an array of the inventors first and last names
|
| 39 | + const finalNameString = inventors.map(inventor => `${inventor.first} ${inventor.last}`); |
| 40 | + console.log(finalNameString); |
37 | 41 |
|
38 | 42 | // Array.prototype.sort()
|
39 | 43 | // 3. Sort the inventors by birthdate, oldest to youngest
|
| 44 | + const sort = inventors.sort(((a, b) => a.year - b.year)); |
| 45 | + console.log(sort); |
40 | 46 |
|
41 | 47 | // Array.prototype.reduce()
|
42 | 48 | // 4. How many years did all the inventors live all together?
|
| 49 | + const year = inventors.reduce((totalYears,x) => { |
| 50 | + return totalYears + (x.passed - x.year); |
| 51 | + }, 0); |
| 52 | + console.log(year); |
43 | 53 |
|
44 | 54 | // 5. Sort the inventors by years lived
|
| 55 | + const sortedYearsLived = inventors.sort(yearsLived); |
| 56 | + |
| 57 | + function yearsLived(a,b) { |
| 58 | + return ((b.passed-b.year)-(a.passed-a.year)); |
| 59 | + } |
| 60 | + |
| 61 | + console.log(sortedYearsLived); |
45 | 62 |
|
46 | 63 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
|
47 | 64 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
|
48 | 65 |
|
49 | 66 |
|
50 | 67 | // 7. sort Exercise
|
51 | 68 | // Sort the people alphabetically by last name
|
| 69 | + const sortedLastName = people.sort(sortByLastName); |
| 70 | + |
| 71 | + function sortByLastName(a,b) { |
| 72 | + return (getLastName(a)>getLastName(b)); |
| 73 | + } |
| 74 | + |
| 75 | + function getLastName(nameValue) { |
| 76 | + let fullName = nameValue.split(", "); |
| 77 | + return fullName[0]; |
| 78 | + } |
| 79 | + |
| 80 | + console.log(sortedLastName); |
52 | 81 |
|
53 | 82 | // 8. Reduce Exercise
|
54 | 83 | // Sum up the instances of each of these
|
55 | 84 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
|
56 | 85 |
|
| 86 | + const dataObjectArray=[] |
| 87 | + for(let i = 0; i<data.length; i++) { |
| 88 | + for (let j = 0; j < dataObjectArray.length; j++) { |
| 89 | + if (dataObjectArray[j].type == data[i]) { |
| 90 | + dataObjectArray[j].amount += 1; |
| 91 | + } |
| 92 | + } |
| 93 | + if(!(dataObjectArray.some(vehicle => vehicle.type == data[i]))) { |
| 94 | + let newVehicle = new Object(); |
| 95 | + newVehicle.type = data[i]; |
| 96 | + newVehicle.amount = 1; |
| 97 | + dataObjectArray.push(newVehicle); |
| 98 | + } |
| 99 | + } |
| 100 | + console.log(dataObjectArray); |
| 101 | + |
| 102 | + |
| 103 | + |
57 | 104 | </script>
|
58 | 105 | </body>
|
59 | 106 | </html>
|
0 commit comments