Skip to content

Commit 03b23ef

Browse files
committed
Solve first 4 problems, give bad answer for wesbos#5
At first I was just going through the exercises and preparing to commit at the very end when everything was fixed, but then I was thinking it'd be neat to show my progress from start to finish of me coming up with a good answer. I started thinking of doing this (committing bad answers) on problem 4 because I tried a few approaches that demonstrated a poor understanding of `Array.prototype.reduce()` -- specifically around the initialValue that should be provided as its second argument. Then I got caught with a misunderstanding around arrow functions! I expected that if my function body were on a new line, so long as the function _itself_ were just one line, it'd return the value and I could omit the explicit `return` statement. I didn't identify the turning points, but now that I write this out, I think it'd be _really_ good for me to make a note of what happened that made me understand better enough to fix my mistakes. In looking back, I think the first turning point was stopping to acknowledge I was confused and looking to the video tutorial's example instead of continuing to "try things." Before I "gave up" I tried simplifying my function to simply increment a variable and return it for each iteration. When that didn't work as expected, I consulted MDN's documentation and learned about the function signature for `Array.prototype.reduce()` -- specfically that it took two arguments: a callback and an "initialValue". When I was surprised the value didn't come back as expected ("undefined" instead of 12), I got confused and pointlessly tried to change some of the specifics around invocation. I moved the reducer function into its own variable. But I was just moving a bad function around, since the issue was with the function syntax itself, specifically that it was on two lines, which made it so it did not implicitly return a value, and I wasn't explicitly calling the `return` statement. So my problem didn't change at all, it just moved. And at that point I was also working in a syntax I disliked -- having the reducer function broken out into its own variable instead of calling it inline. I began to resent my own code and fell into a Dostoevskian hell of self-loathing. (Okay, maybe not, but it distracted me.) I tried logging things in the function, and although it's tough to identify what made me start doing it, I called `return` in the function body and started seeing things change. Eventually I got myself back on track. Notes on the failed solution to exercise 5: At this point, I've tried tackling the problem in just one line very similarly to how I'm solving in exercise 3. This returns an array of "[object Object]"s which is totally not what I'm going for. I think what's happening is the type is being coerced to a string by the backticks. I bet if I break this out into its own console log, it will preserve the data as an object.
1 parent b5a9811 commit 03b23ef

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,35 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
console.log(
35+
inventors.filter((inventor) => inventor.year >= 1500 && inventor.year < 1600)
36+
);
3437

3538
// Array.prototype.map()
3639
// 2. Give us an array of the inventors' first and last names
40+
console.log(
41+
inventors.map((inventor) => `${inventor.first} ${inventor.last}`)
42+
);
3743

3844
// Array.prototype.sort()
3945
// 3. Sort the inventors by birthdate, oldest to youngest
46+
console.log(
47+
inventors.sort((thisInventor, lastInventor) => thisInventor.year - lastInventor.year)
48+
);
4049

4150
// Array.prototype.reduce()
4251
// 4. How many years did all the inventors live?
52+
console.log(
53+
`4. How many years did all the inventors live?
54+
${inventors.reduce((accumulator, inventor) => accumulator += (inventor.passed - inventor.year), 0)} years`
55+
);
4356

4457
// 5. Sort the inventors by years lived
58+
console.log(
59+
`5. Sort the inventors by years lived
60+
${inventors.sort((thisInventor, lastInventor) => (thisInventor.passed - thisInventor.year) - (lastInventor.passed - lastInventor.year))}
61+
`
62+
)
4563

4664
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4765
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris

0 commit comments

Comments
 (0)