Thursday, October 26, 2017

Day One: Zero-Balanced Arrays drive me to bake corn muffins

So this morning my JavaScript mentor, Tom Kraak, messaged me to say that he is meeting with his manager today -- and that he wanted my permission to mention me as a candidate for the JS dev hires his company is making. Holy shit. The imposter syndrome was strong. I feel like I am nowhere near ready. I said this. Tom replied that I am ready. There was some more "no I'm not, yes you are" back and forth, but the upshot is he told me it's time to jump off the cliff and I told him to go ahead and throw my name into the ring. (I am nothing if not a mistress of the mixed-up metaphors).

Aaaaand then I proceeded to have a panic attack over how I  have literally zero anything to show a prospective boss. None of my projects are finished. Four websites all about 80% done. A couple JS projects -- a pomodoro clock, your classic todo list "app" -- both lingering in the twilight zone limbo of almost but not quite finished.  OK! So! Clearly it's time to dust off my hands and get to work finishing...so of course first I decide to do a quick code challenge


Codewars, level 7 kyu (out of 8 levels, 8 being the easiest), "zero-balanced sum" challenge. Seemed like it would not present too much trouble:



"An array is called zero-balanced if its elements sum to 0 and for each positive element n, there exists another element that is the negative of n. Write a function named ìsZeroBalanced that returns true if its argument is zero-b"alanced array, else return false. Note that an empty array will not sum to zero."

I was determined to tackle this in Grown Up JS -- no for loops or if/else statements.  And quickly settled on my approach:

1. Check for empty array. If empty, return false.
2. Use reduce() to sum array's contents to a single value.
3. (here's the tricky part) Iterate over the array to match positive and negative values. In a true zero balanced array, each positive integer item will have a corresponding negative integer item. A scroll thru MDN turned up Array.prototype.includes() and Array.prototype.every() combined should do the trick.

SO, I ended up with

which should work, right?

Only it didn't.  And I spent four hours, including an appeal to Stack Overflow, trying to figure out why.

I haaaaate asking Stack Overflow for help. Those people are like my ex -- ignoring the question I'm asking in order to criticize, nitpick and start a fight over HOW I'm asking it.

And they were useless, anyway.  Two hours later, after a lot of cursing at the screen and a break to bake some corn muffins, because at least I know how to bake corn muffins without actually FUCKING IT ALL TO HELL, I finally tried:



And IT WORKED.

Can you see the subtle difference?

It was all in the variable names in the every() callback function.  I used n because apparently I am an idiot.  I was so worried to bits about getting the syntax right that when things refused to work I refused to believe the problem was anything BUT my syntax. It didn't help that the error that was getting thrown in console was


"Uncaught TypeError: n.every is not a function"

Y not?

Oh ho ho hohooooo. You laugh, or you cry. When it comes to learning JavaScript, sometimes you do both at the same time.

Tomorrow I am going to take a crack at refactoring the answer to remove the if/else statement into a ternary. But for now I'm just so very very very happy that it works, I fear to touch one single thing.

No comments:

Post a Comment