|
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 | + console.log('#1. Filter'); |
| 35 | + console.log( |
| 36 | + inventors.filter( inventor => { |
| 37 | + return (inventor.year >= 1500 && inventor.year < 1600); |
| 38 | + }) |
| 39 | + ); |
34 | 40 |
|
35 | 41 | // Array.prototype.map() |
36 | 42 | // 2. Give us an array of the inventors first and last names |
| 43 | + console.log('#2. Map'); |
| 44 | + console.log( |
| 45 | + inventors.map( inventor => { |
| 46 | + return { |
| 47 | + first: inventor.first, |
| 48 | + last: inventor.last |
| 49 | + } |
| 50 | + }) |
| 51 | + ); |
37 | 52 |
|
38 | 53 | // Array.prototype.sort() |
39 | 54 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 55 | + console.log('#3. Sort'); |
| 56 | + console.log( |
| 57 | + inventors.sort( (a, b) => { |
| 58 | + if (a.year > b.year) { |
| 59 | + return 1; |
| 60 | + } else if (a.year < b.year) { |
| 61 | + return -1; |
| 62 | + } else { |
| 63 | + return 0; |
| 64 | + } |
| 65 | + }) |
| 66 | + ); |
40 | 67 |
|
41 | 68 | // Array.prototype.reduce() |
42 | 69 | // 4. How many years did all the inventors live all together? |
| 70 | + console.log('#4. Reduce'); |
| 71 | + console.log( |
| 72 | + inventors.reduce((sum, inventor) => { |
| 73 | + console.log(sum, inventor); |
| 74 | + return (sum + (inventor.passed - inventor.year)); |
| 75 | + }, 0) |
| 76 | + ) |
43 | 77 |
|
44 | 78 | // 5. Sort the inventors by years lived |
| 79 | + console.log('#5. Sort 2'); |
| 80 | + console.log( |
| 81 | + inventors.sort( (a, b) => { |
| 82 | + if ((a.passed - a.year) > (b.passed - b.year)) { |
| 83 | + return 1; |
| 84 | + } else if ((a.passed - a.year) < (b.passed - b.year)) { |
| 85 | + return -1; |
| 86 | + } else { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + }) |
| 90 | + ) |
45 | 91 |
|
46 | 92 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
47 | 93 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
48 | 94 |
|
49 | 95 |
|
50 | 96 | // 7. sort Exercise |
51 | 97 | // Sort the people alphabetically by last name |
| 98 | + console.log('#7. Sort Last Name'); |
| 99 | + console.log( |
| 100 | + people.sort((a, b) => { |
| 101 | + let a_lastName = a.split(', ')[1]; |
| 102 | + let b_lastName = b.split(', ')[1]; |
| 103 | + if (a_lastName > b_lastName) { |
| 104 | + return 1; |
| 105 | + } else if (a_lastName < b_lastName) { |
| 106 | + return -1; |
| 107 | + } else { |
| 108 | + return 0; |
| 109 | + } |
| 110 | + }) |
| 111 | + ); |
52 | 112 |
|
53 | 113 | // 8. Reduce Exercise |
54 | 114 | // Sum up the instances of each of these |
|
0 commit comments