Skip to content

Instantly share code, notes, and snippets.

@pernstrong
Last active February 21, 2020 17:46
Show Gist options
  • Save pernstrong/de9705603175bb193f8f627e484ae8a6 to your computer and use it in GitHub Desktop.
Save pernstrong/de9705603175bb193f8f627e484ae8a6 to your computer and use it in GitHub Desktop.

A Tale of a Partner Code Challenge

The Challenge:

Not all dragons breathe fire and live in mountain caves. Some dragons are coding challenges and are slayed with functions. This story involves a dragon of that type.

The Dragon:

Write a function that takes an Array of Strings. This function should return an array of the same strings that were in the original array. However, the returned array should hold the strings in reverse alphabetical order.

var words = ['hi',  'hello', 'carrot', 'world'];
reverseAlphaWords(words);
// => ['world', 'hi', 'hello', 'carrot']

The Setup

It was dark and dreary morn, the 21st of February, the year of our Lord 2020, when Quinn Elder the Welder and Dave the Brave tackled the impossible task of slaying the dragon. The stakes were high...failure was not an option. If these two could not get the array of strings into reverse alphabetical order the entire kingdom would be in ruins...

Chapter One: The Pseudo Phase

It started, as it often does, with a bit of pseudo-coding. The duo used their sacred whiteboards and dry-erase markers to list out the steps of the journey they would take. One by one the pieces were laid out and the tasks were set. To begin they would need to create a function. This function would need to compare the individual words of the string of arrays then sort them alphabetically. Next, the order of the alphabetized array would need to be....REVERSED! Many ideas of how to go about this were mulled about. The original plan was to create a function to compare the first letter, and subsequent letters if needed, to see which one came first in the alphabet. This would be no easy task but the Dynamic Duo was ready for the challenge to save their kingdom. With the plan in place, Quinn and Dave began the next phase...

Chapter Two: The Gathering of Intel

No lancer of any note competes in a jousting match without a lance. No dragon slayer challenges a dragon without a sword. This is known throughout the land. The same holds true for solving a coding challenge...the best tools available are needed. For this reason, Quinn Elder the Welder and Dave the Brave spent a significant amount of time researching the different methods that would be available for them. The options of tools was tremendous. Would a string prototype be needed? Or some sort of array method? Would the duo need to go forward with their plan to iterate through ALL letters of each string in the array and compare them? ALAS, the duo came across the King Arthur's Sword of array prototypes...the SORT method. The .sort() method would alleviate the need for the loop comparing letters and by some sort of wizards magic it would sort the entire array alphabetically, by string. Then, they would only need the .reverse() method which they had trained with as young peasants to complete the challenge.

Chapter Three: The Battle

Armed with the array prototypes the duo set out on their journey to behead the impossible dragon. A repl was created, pseudo code entered as comments, and the function began.. First a function declaration...then a name...none other than reverseAlphaWords() was chosen and a parameter set. The duo then called their array prototypes, .sort() and .reverse() and returned the value within the function. The repl was run and the moment of truth awaited....

The function was called with an array of strings as its argument. The entire kingdom waited in silence as the return button was entered and their fate was being deteremined. SUCCESS! The array of words came back in reverse alphabetical order!

var words = ['hi', 'hello', 'zoo', 'apple', 'carrot', 'world'];

function reverseAlphaWords(array) {
  array.sort()
  array.reverse()
  return array
}
 reverseAlphaWords(words);
[ 'zoo', 'world', 'hi', 'hello', 'carrot', 'apple' ]

Chapter Four: The Final Chapter

Celebrations and feasts were held throughout the kingdom and the duo was given the highest honors of the land. They were regaled as the greatest code-crackers and dragon-slayers of all time. As the sun set on the journey there was just one more thing to do...add another test case. The natural thing to do was to add a name to the array of strings to test it out one final time, to ensure the dragon was slain. This would add both capital letters and spaces. Naturally, the duo added 'Jude Law' to their string and ran the test...

 reverseAlphaWords(words);
[ 'zoo', 'world', 'hi', 'hello', 'carrot', 'apple', 'Jude Law' ]

FAILURE! The capital letter took precidence over the other letters and was listed first. Like a pheonix the dragon arose from the dead. The duo was saddened but would never give up. A string method of toLowerCase() and some loops would be needed. The battle will be continued...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment