|
11 | 11 | // ## Array Cardio Day 1 |
12 | 12 |
|
13 | 13 | // Some data we can work with |
| 14 | + // REPEAT THSES UNTIL CONFIDENT AND ABLE TO GO TO A WEBSITE AND REPLICATE |
14 | 15 |
|
15 | 16 | const inventors = [ |
16 | 17 | { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 }, |
|
34 | 35 | 'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose', |
35 | 36 | 'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank' |
36 | 37 | ]; |
37 | | - |
| 38 | + |
38 | 39 | // Array.prototype.filter() |
39 | 40 | // 1. Filter the list of inventors for those who were born in the 1500's |
| 41 | + const fifteen = inventors.filter(function(inventor) { |
| 42 | + if(inventor.year >= 1500 && inventor.year < 1600) { |
| 43 | + return true; //keeps the ones that are true |
| 44 | + } |
| 45 | + }); |
| 46 | + console.table(fifteen); |
40 | 47 |
|
41 | 48 | // Array.prototype.map() |
42 | 49 | // 2. Give us an array of the inventors first and last names |
| 50 | + const fullNames = inventors.map(inventor => inventor.first + ' ' + inventor.last); |
| 51 | + console.log(fullNames); |
| 52 | + // another way to do it const fullNames = inventors.map(inventor => `${inventor.first}` `$inventor.last`); |
43 | 53 |
|
44 | 54 | // Array.prototype.sort() |
45 | 55 | // 3. Sort the inventors by birthdate, oldest to youngest |
| 56 | + // const ordered = inventors.sort(function(firstPerson, secondPerson) { |
| 57 | + // if(firstPerson.year > secondPerson.year) { |
| 58 | + // return 1; |
| 59 | + // } else { |
| 60 | + // return -1; |
| 61 | + // } |
| 62 | + // }); |
| 63 | + const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1); |
| 64 | + |
| 65 | + console.table(ordered); |
46 | 66 |
|
47 | 67 | // Array.prototype.reduce() |
48 | 68 | // 4. How many years did all the inventors live all together? |
| 69 | + const totalYears = inventors.reduce((total, inventor) => { |
| 70 | + return total + (inventor.passed - inventor.year); |
| 71 | + }, 0); |
| 72 | + console.log(totalYears); |
49 | 73 |
|
50 | 74 | // 5. Sort the inventors by years lived |
51 | | - |
| 75 | + const oldest = inventors.sort(function(a, b) { |
| 76 | + const lastGuy = a.passed - a.year; |
| 77 | + const nextGuy = b.passed - b.year; |
| 78 | + return lastGuy > nextGuy ? -1 : 1; |
| 79 | + }); |
| 80 | + console.table(oldest); |
52 | 81 | // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
53 | 82 | // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
54 | | - |
| 83 | + //code run in the browser |
| 84 | + // const category = document.querySelector('.mw-category'); |
| 85 | + // const links = Array.from(category.querySelectorAll('a')); |
| 86 | + // const de = links |
| 87 | + // .map(link => link.textContent) |
| 88 | + // .filter(streetName => streetName.includes('de')); |
55 | 89 |
|
56 | 90 | // 7. sort Exercise |
57 | 91 | // Sort the people alphabetically by last name |
| 92 | + const alpha = people.sort(function(lastOne, nextOne) { |
| 93 | + const [aLast, aFirst] = lastOne.split(', '); |
| 94 | + const [bLast, bFirst] = nextOne.split(', '); |
| 95 | + return aLast > bLast ? 1 : -1; |
| 96 | + }); |
| 97 | + console.log(alpha); |
58 | 98 |
|
59 | 99 | // 8. Reduce Exercise |
60 | 100 | // Sum up the instances of each of these |
61 | 101 | const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; |
62 | 102 |
|
| 103 | + const transportation = data.reduce(function(obj, item) { |
| 104 | + if(!obj[item]){ |
| 105 | + obj[item] = 0; // |
| 106 | + } |
| 107 | + obj[item]++; //iterates here |
| 108 | + return obj; |
| 109 | + }, {}); //<-- the empty {} are a blank object that we start with. The iterate and count the amount of each object is present in the array data |
| 110 | + console.log(transportation); |
63 | 111 | </script> |
64 | 112 | </body> |
65 | 113 | </html> |
0 commit comments