You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
0 commit comments