Skip to content

Commit

Permalink
Solve first 4 problems, give bad answer for wesbos#5
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ianjmacintosh committed Jan 19, 2019
1 parent b5a9811 commit 03b23ef
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 04 - Array Cardio Day 1/index-START.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,35 @@

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

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

// Array.prototype.sort()
// 3. Sort the inventors by birthdate, oldest to youngest
console.log(
inventors.sort((thisInventor, lastInventor) => thisInventor.year - lastInventor.year)
);

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

// 5. Sort the inventors by years lived
console.log(
`5. Sort the inventors by years lived
${inventors.sort((thisInventor, lastInventor) => (thisInventor.passed - thisInventor.year) - (lastInventor.passed - lastInventor.year))}
`
)

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

0 comments on commit 03b23ef

Please sign in to comment.