CoDEVIANT #27 — Goldilocks, Kit-Kat Bars, & the Three Integers

Adrian Rosales
5 min readNov 19, 2020

--

Did you know that integers can swallow children whole? They also roam national parkgrounds and swipe peoples’ pic-nic baskets.

Just pulling the ol’ 1,2,3,4,5,6,7,8,9….y’know the rest

Well today we’re going to solve a neat little problem. I have recently discovered my developer super power. As an extremely visual person who suffers from not always having a largess of working memory, I find it helpful to draw pictures. Seriously, little hieroglyphics representing images of what I want to achieve in steps really makes a huge difference for me. I feel like my mind is freed up to focus on each step of a problem without having to actively focus on keeping the last thing in my head from getting mixed up.

Now I’m thinking of “Take on Me” by A-ha.

The problem we are solving today is to write a function that accepts an array of integers as an argument. The function must return an array containing the three largest integers in the array, in ascending order, without using the sort method on the given array.

So here is how I solved that sucker.

function findThreeLargestNumbers(array) {
let piece = array.splice(0,3)
doTheHustle(piece)for( idx in array) {
let el = array[idx]
goldilocks(el, piece)
}

return piece
}
function goldilocks(el, piece) {
doTheHustle(piece)
if(el > piece[2]) {
let b = piece[2]
let a = piece[1]
piece[2] = el
piece[1] = b
piece[0] = a
return
}
if(el > piece[1]) {
let a = piece[1]
piece[1] = el
piece[0] = a
return
}
if(el > piece[0]) {
piece[0] = el
return
}
}
function doTheHustle(piece) {
for(let i = 0; i <= piece.length -1 ; i++) {
if(piece[i] > piece [i + 1]) {
let tmp = piece[i]
piece[i] = piece[i + 1]
piece[i +1] = tmp
}
}
}

So there’s a little bit going on here, it requires two helper methods. Let’s break it down shall we? First we’ll take a look at the main method:

function findThreeLargestNumbers(array) {
let piece = array.splice(0,3)
doTheHustle(piece) for( idx in array) {
let el = array[idx]
goldilocks(el, piece)
}

return piece
}

We first create a new array for the variable piece by getting the first three elements of the given array and cutting them away from the given array argument.

let piece = array.splice(0,3)
We’re literally doing this to the ‘array’ argument.

Next, we call the method doTheHustle on the piece array we just made. We’ll get into the implementation later, but what this method does is sort the three elements of the piece array by checking to see if the first element is larger than the second element and then re-setting the elements if so (it then does the same with the 2nd and 3rd elements).

doTheHustle(piece)

Next we use a for…in statement on the array argument (which, do recall has lost its first three members). We say that “for every index in array, do the following”. The statement takes the integer at the array[index] position and the the piece array and sets them as arguments in the goldilocks method. Like the doTheHustle method, we’ll get into the implementation of that in a bit, but to be succint, the goldilocks method checks each of the existing elements in piece and if the new integer passed in is larger than any of the elements in piece temporary variables are created to facilitate the shifting of the elements in piece.

for( idx in array) {
let el = array[idx]
goldilocks(el, piece)
}

Finally, we return piece.

return piece

Okay so before we can call it a day and go home, it’s important that we understand what’s going on with doTheHustle and goldilocks.

I don’t know what’s up with me and why I’m invoking fucking disco. *shrugs*

Sigh…let’s doTheHustle shall we?

function doTheHustle(piece) {
for(let i = 0; i <= piece.length - 1 ; i++) {
if(piece[i] > piece [i + 1]) {
let tmp = piece[i]
piece[i] = piece[i + 1]
piece[i + 1] = tmp
}
}
}

Basically we have a for loop that does the code-block as long as i is less than or equal to the length of piece minus 1. Since our piece will only ever be 3 elements long, this means that we will be managing the indices 0, 1, and 2. (Cuz y’know how arrays are zero-indexed).

for(let i = 0; i <= piece.length - 1 ; i++) { ... }

So inside the for-loop we say that: “If the number at piece[i] is larger than the following number in piece (which lives at piece[i + 1]), then we:

  • Create a variable called tmp which holds the current value of piece[i]
  • Set piece[i] to equal piece[i + 1]
  • Set piece[i + 1] to equal whatever value is stored in tmp
if(piece[i] > piece [i + 1]) {
let tmp = piece[i]
piece[i] = piece[i + 1]
piece[i + 1] = tmp
}

It’s important to note that in doTheHustle, no elements are discarded from whatever the line-up of integers is inside of piece. That action takes place in goldilocks method.

function goldilocks(el, piece) {
doTheHustle(piece)

if(el > piece[2]) {
let b = piece[2]
let a = piece[1]
piece[2] = el
piece[1] = b
piece[0] = a
return
}
if(el > piece[1]) {
let a = piece[1]
piece[1] = el
piece[0] = a
return
}
if(el > piece[0]) {
piece[0] = el
return
}
}

Right off the top, I call doTheHustle because I want to make sure that everything is sorted so that the functionality of this method yields the appropriate result.

doTheHustle(piece)

Then we go through a series of if statements that compare the value of the el integer from the for…in statement against the elements in piece.

If el is larger than the last element of piece → piece[2]:

  • We create a variable b to contain piece[2]
  • We create a variable a to contain piece[1]
  • We set piece[2] to equal el
  • We set piece[1] to equal b
  • We set piece[0] to equal a
  • Then we return
if(el > piece[2]) {
let b = piece[2]
let a = piece[1]
piece[2] = el
piece[1] = b
piece[0] = a
return
}

If el is larger than the second element of piece → piece[1]:

  • We create variable a to equal piece[1]
  • We set piece[1] to equal el
  • We set piece[0] to equal a
  • Then we return
if(el > piece[1]) {
let a = piece[1]
piece[1] = el
piece[0] = a
return
}

If el is larger than the first element of piece → piece[0]:

  • We set piece[0] to equal el
  • Then we return

There’s no need to mess around with anything else!

if(el > piece[0]) {
piece[0] = el
return
}

And as they say in showbiz and the mummifying business…

to be clear, not a WAP…totally different content, go check out my non-existent OnlyFans for that XD

--

--

Adrian Rosales
Adrian Rosales

Written by Adrian Rosales

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

No responses yet