Skip to content

Commit b9b6ef0

Browse files
committed
whew. array cardio is awesome and i think i need to pause here and just work with arrays and these methods till i know what they do and when to use them
1 parent 616976c commit b9b6ef0

1 file changed

Lines changed: 73 additions & 4 deletions

File tree

04 - Array Cardio Day 1/index-START.html

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,89 @@
7878
console.log('ordered: ', ordered);
7979
// Array.prototype.reduce()
8080
// 4. How many years did all the inventors live?
81+
/*
82+
like the other array methods, reduce loops over every
83+
item in our array.
84+
the first argument of reduce represents what you returned
85+
from the function last time (accumulator), we can call that total
86+
and it is also going to give you the inventor (currentValue)
87+
*/
88+
// const totalYears = inventors.reduce((total, inventor) => {
89+
// return total + (inventor.passed - inventor.year);
90+
// });
91+
/*
92+
at this point in our code we are almost there but if you
93+
look at this log we get back [object Object]7859843780508967907681
94+
this is because the first time reduce loops we dont have a total
95+
and its equal to undefined. so what we need to do is use the second
96+
argument (initialValue) to reduce with the value 0. this means reduce
97+
will use 0 as the initial value so we will not get undefined anymore
98+
and our years will properly add up.
99+
*/
100+
const totalYears = inventors.reduce((total, inventor) => {
101+
return total + (inventor.passed - inventor.year);
102+
}, 0);
103+
104+
console.log('totalYears: ', totalYears);
81105

82106
// 5. Sort the inventors by years lived
83107

108+
const oldest = inventors.sort((a, b) => {
109+
const lastGuy = a.passed - a.year;
110+
const nextGuy = b.passed - b.year;
111+
return lastGuy > nextGuy ? -1 : 1;
112+
});
113+
console.log('oldest: ', oldest);
114+
84115
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
85116
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
86-
87-
117+
// use this js in the console of the wiki page
118+
119+
// const links = category.querySelectorAll('a');
120+
// qSA returns a nodelist but only forEach is available
121+
// on nodeLists so lets turn it into an array so we can run
122+
// map on it, first, wrap it in an array then we can use es6
123+
// spread operator to spread each item into the array
124+
// into the array.
125+
// we can also simply wrap it in Array.from(category.quer...)
126+
// i like using es6 so im using that one, even tho Array.from
127+
// reads better
128+
// const category = document.querySelector('.mw-category');
129+
// const links = [...category.querySelectorAll('a')];
130+
// const de = links
131+
// .map(link => link.textContent)
132+
// .filter(streetName => streetName.includes('de'));
133+
// console.log('de: ', de);
88134
// 7. sort Exercise
89135
// Sort the people alphabetically by last name
90-
136+
const alpha = people.sort((lastOne, nextOne) => {
137+
const [aLast, aFirst] = lastOne.split(', ');
138+
const [bLast, bFirst] = nextOne.split(', ');
139+
console.log('aLast, aFirst: ', aLast, aFirst);
140+
console.log('bLast, bFirst: ', bLast, bFirst);
141+
return aLast > bLast ? 1 : -1;
142+
// console.log('lastOne: ', lastOne);
143+
// console.log('nextOne: ', nextOne);
144+
});
145+
console.log('alpha: ', alpha);
91146
// 8. Reduce Exercise
92147
// Sum up the instances of each of these
93148
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
94-
149+
// start our reduce with an empty object that we add to
150+
// as seen in second argument in the reduce method
151+
const transportation = data.reduce((obj, item) => {
152+
// console.log('transportation item: ', item);
153+
// what we want to do is to increment our object key
154+
// so we can tally how many instances of the key there are
155+
// we need to check first if there is an item of an obj or not
156+
// if it doesnt we can set it to 0 and increment and return it
157+
if (!obj[item]) {
158+
obj[item] = 0;
159+
}
160+
obj[item]++;
161+
return obj;
162+
}, {});
163+
console.log('transportation: ', transportation);
95164
</script>
96165
</body>
97166
</html>

0 commit comments

Comments
 (0)