On object manipulation in JavaScript

Honesty is important to me, even when it means coming clean about things I struggle with. Today, I confess that I struggle with comprehending object manipulation in JavaScript. Earlier this morning I was pairing with a friend on some katas in Code Wars, and I was struggling to understand what was happening with this code:

let staff = {
  tim: 'finance',
  jim: 'accounts',
  randy: 'canteen'
}

function boredom(staff) {
  var map = {
    accounts: 1,
    finance: 2,
    canteen: 10
 }

  var score = Object.keys(staff).reduce((a, b) => {
    return a + map[staff[b]]
  }, 0)

  return score <= 13 ? 'kill me now' : 'party time!!'
}

As you can see, boredom is taking in the staff object, where it wants to match up the staff values with the corresponding numbers in the map object, then tally the numbers to produce a score and return that score’s relevant string. So theoretically I understood all that, but not this specific part:

map[staff[b]]

My brain was reading it like this…

map.staff.b

…which is sort of right, but also why I was getting confused.
I mean, map and staff are two separate objects, not connected in any way, so how is that working? Let’s walk through it! And by let’s I mean me, so I can solidify my understanding 🙂

Alright, so the first time through, a is 0 and b is ‘tim.’ Which means map[staff[b]] is basically map['tim']. And what’s Tim’s value? It’s finance. Since Tim’s value is ‘finance,’ map['tim'] is basically map.finance. And what is map.finance? That’s right, it’s 2! So the first time through, 2 gets added to 0, and on the next time through 2 becomes a, and b is now ‘jim,’ and on and on until the numbers are reduced down and boredom spits out how you feel about your office’s fun level.

Check out the reduce method on MDN.

Find out more about objects in Eloquent JavaScript.

And don’t forget, you can walk through code step-by-step on this site.

That time my blog got hacked (or did it?)

You may notice there’s a bit of a lapse in time between this post and the one before. You may be wondering what happened to weeks six through twelve. Well, let me tell you a little story.

Bootcamp got crazy about halfway through and I could not keep up on writing posts. Every week, I would take notes and compose a quick draft, but could never find the time (or leftover brain power) to polish the piece.

Fast forward to the end of school (middle of December). We were given three weeks to create our final project and there I was, four days before demo day and thinking, “I finished early! Yay! Now I have time to get my blog up to date!” I try to log in and can’t. I quickly figure out my site has been blacklisted for phishing, which I find hilarious because I don’t have users on my blog, I don’t allow comments, etc. – it’s mostly just a place for me to lay down my thoughts and show people that I can indeed put sentences together. So, good job phishers! You picked a winner! I find out it takes about $200 to get a site ‘cleaned,’ which I can’t justify spending. I turn to the AMAZING Utah JavaScript community for input, and a complete stranger offers to log in to my site and clean it for free. Thing is, he can’t find any malicious code at all once he gets in there. So we ask Google to review it, and they say, “Nah, it’s still blacklisted.”

Meanwhile, school is over, then the holidays come and go, then job hunting begins in earnest. Why yes, I am still paying monthly hosting and domain fees, because annual contracts, don’t ya know! I spend a lot of my time coding and decide that since I can’t have the site cleaned, maybe I’ll just copy what I can to a web app I built myself. So just a few days ago I pull up my site to find a posted date, and what do you know, it’s not blacklisted anymore!! And there they all are, my 18 or so drafts waiting to be finished and published. Which maybe I’ll do someday, after I have a job 🙂