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

`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  } }}`
`function findThreeLargestNumbers(array) { let piece = array.splice(0,3) doTheHustle(piece) for( idx in array) {  let el = array[idx]  goldilocks(el, piece) }  return piece}`
`let piece = array.splice(0,3)`
`doTheHustle(piece)`
`for( idx in array) {  let el = array[idx]  goldilocks(el, piece) }`
`return piece`
`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  } }}`
`for(let i = 0; i <= piece.length - 1 ; i++) { ... }`
• 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}`
`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 }}`
`doTheHustle(piece)`
• 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 }`
• 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 }`
• We set piece[0] to equal el
• Then we return
`if(el > piece[0]) {  piece[0] = el  return }`

--

--

--

## More from Adrian Rosales

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

Love podcasts or audiobooks? Learn on the go with our new app.