Following through with learning in public

Following through with learning in public

Closing out the month of June on a strong note, slowly rebuilding the streak of daily tasks: reviewing my Anki deck and completing at least one Code Wars challenge and pushing it to GitHub. Going into this post, I wanted to acknowledge having dropped the ball on the whole point of having this blog (as I've always seemed to address whenever I fall out of a commitment or habit). But this time around, I'm going to focus my efforts on things that only serve in my progression instead of giving attention to the things that don't, like beating myself up for being human. Now, onward!

Since my last post, I've also created a separate repo on my GitHub called Today I Learned. Although very similar in nature to this coding blog, my intention for that repo is to write shorter form content, almost like a second brain for myself for when I need to reference knowledge in an instant.

Tonight's Code War challenge was to find the middle element within an array containing 3 numbers. The task at hand seemed simple enough, and to further drill in good learning concepts I wrote the pseudo-code down as such:

  • Iterate through the array and sort them in ascending order
  • Target the element at index 1
  • Use arr.indexOf() referencing the targeted number

Confident in my approach, I used methods I'm familiar with to complete each task.

const gimme = triplet => {
  let copy = triplet.sort((a, b) => a - b) 
  // sort array into ascending order
  let target = copy[1] 
  // target the element in the middle
  return triplet.indexOf(target) 
  // return the index of the element matching target
}

Although the above code executed without throwing any errors, the index being returned always resulted in 1.

After much wrestling with the code, trying to use other array methods such as .filter() and .map() to try and isolate the index of target, I couldn't quite understand why it was giving me the incorrect index. I then realized that my error started at the very beginning of my approach – although I thought I was creating an entirely separate array through variable assignment of copy, I learned the hard way that using .sort() modifies the original array. To avoid modifying the original array, I needed to utilize one more method: .splice(). Here's the code that ended up working:

const gimme = triplet => {
  let copy = triplet.splice() 
  // using this method without passing any parameters creates an identical copy of the array
  let target = copy.sort((a, b) => a - b)[1] 
  // doesn't modify the original triplet array, and referenced the element at index 1 in one line
  return triplet.indexOf(target)
}

That moment where the light bulb turns on 💡 is a feeling like no other, and I knew this was very much a learning moment that deserved to be blogged about. I know there's a lot of theory that can be learned in this moment (does any of this have to do with JavaScript's pass by reference vs. pass by value?), but I know it first starts with learning out loud, in public, like I said I'd do.

Have you tried this Code War challenge out, or maybe solved a similar problem? What was your approach, or what a-ha moments have you found with using .sort()?